Skip to content
Advertisement

children prop in React component

i am learning react at the moment. this is the link with the code – http://redux.js.org/docs/basics/ExampleTodoList.html

I am having a bit of difficulty understanding what’s going on in this part of the code

JavaScript

I am having the most difficulty understand this snippet

JavaScript

What does {children} mean here? What does it do?

and what does this do?

JavaScript

what is meant by node in the above line?

Advertisement

Answer

When you use a Custom component, like

JavaScript

Whatever you write between the tags (in above case Hello World) is passed to the component as a children prop.

So when write your component like

JavaScript

You are destructuring the props and getting only active, children and onClick from the props passed to the component

Consider for example, you call the Link component like

JavaScript

Then amongst all the props i.e active, onClick, style, children, you will only be accessing active, onClick,children in the component.

For your second question:

and what does this do?

children: PropTypes.node.isRequired,

So here PropTypes is a way of performing a typeCheck on the props that are passed to the component. It is being imported from the react-proptypes package.

So

JavaScript

makes the prop children to be required. So if your render your component like

JavaScript

It will not pass the type check and hence you need to do

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement