skip to Main Content

I’m confused on these different uses of children and why they work. Can anyone explain? Is children a default parameter for react native functions?

const Component = ({
  children = <Text>Insert Icon</Text>,
}) => {
  return ( {children} );
};

const Component = ({
  {children},
}) => {
  return ( {children} );
};

Not a problem, just want an explanation

2

Answers


  1. Is children a default parameter for react native functions?

    Yes. You can set default value for parameters in function:

    const multiply = (a, b = 1) => {
      return a * b;
    }
    
    console.log(multiply(5, 2));
    // Expected output: 10
    
    console.log(multiply(5));
    // Expected output: 5
    

    More info: docs

    Login or Signup to reply.
  2. when you set

    ({
      children = <Text>Insert Icon</Text>,
    })
    

    it’s mean if children = null, hence children = <Text>Insert Icon</Text>

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search