Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't children be passed as a prop?

Tags:

reactjs

eslint

According to ESLINT rule no-children-prop

When using JSX, the children should be nested between the opening and closing tags. When not using JSX, the children should be passed as additional arguments to React.createElement

Is this just a style suggestion, or can real damage arise?

like image 731
jrhicks Avatar asked Mar 23 '17 16:03

jrhicks


People also ask

Can you pass children as a prop?

As said, there is no way passing props from a child to a parent component. But you can always pass functions from parent to child components, whereas the child components make use of these functions and the functions may change the state in a parent component above.

What is the purpose of children prop?

children is a special prop, automatically passed to every component, that can be used to render the content included between the opening and closing tags when invoking a component.

What is the difference between props and children?

Children are props If you want to pass things to this button, you would use a prop. If you want to make our button say more than just "I am a button," you can pass children to it. By passing children in this way, you are passing it to the component by position.

Can you pass props back to parent?

To pass data from a child component to its parent, we can call a parent function from the child component with arguments. The parent function can be passed down to the child as a prop, and the function arguments are the data that the parent will receive.


1 Answers

It seems to be more of a stylistic suggestion and also prevents you from shooting yourself in the foot.

If you had a component:

<Component children="foo">bar</Component>

and in the component do

return (
  <div>
    {this.props.children}
  </div>
)

you would end up getting bar as this.props.children instead of foo but as far as this breaking anything in react or causing "damage" I have found no evidence of such and there is a lengthy discussions here https://github.com/airbnb/javascript/issues/1089#issuecomment-250640742 stating the same.

like image 113
finalfreq Avatar answered Sep 21 '22 18:09

finalfreq