React. Children
React. Children can only receive a single React element child. This is because of React. Child is a mechanism for dealing with the different types of children a component can receive. To deal with multiple children, you need to use React.A fragment or use an array.
What is React. Children?
React. Children provide a way to manipulate children in a way that is agnostic to the type of children being passed in. React. Children only return a single react element child, even if you pass in multiple children. If you pass in multiple children, React. Children will return the first child only. To use React. Children, you must wrap your application in a provider component. For example:
const Provider = props => (
{props.children}
);
class App extends React.Component {
render() {
return (
App // only this div will be returned by React.Children Another Div // this div will not be returned because it is not the first child
);
}
}
Why is React.Children important?
React.Children is important for several reasons:
React.Children lets you iterate over children that may have different types. If you have a mixed list of children, some of which are JSX elements and some of which are strings, you’ll want to use React.Children.map rather than just iterating over the array. This gives you extra flexibility in terms of what you can do with each child, and how you want to render each child.
React.Children is also used internally by React when it needs to pass props down to a child component. In particular, if a component has static props that should be passed down to its children, React uses React.Children to do that automatically.
Finally, React provides some utility functions for working with children that are only available through the React.Children namespace. For example, React.Children.only can be used to ensure that a particular component only has one child (which must be an element), and will throw an error if that’s not the case.
The only React.Children is a single React element child
React.Children.only() expects to receive a single React element child. error when trying to access an array of children.
What does this error mean?
This error occurs when you pass more than one child element to a component expecting only a single child.
React.Children only accepts a single React element child. If you’re passing more than one, you’ll get this error.
This error is usually caused by passing multiple children to a component that doesn’t accept them. For example, the component expects only one direct child – the components that will be rendered within it. If you pass multiple elements as children, you’ll get this error.
To fix this, make sure you’re only passing a single React element as a child to the component in question. In the case of , that would mean rendering only one directly within it:
How can I fix it?
If you are using React.Children.only(props.children) and you get the error: “reactchildrenonly expected to receive a single react element child”, it means that you are passing in more than one child to a component that can only have one.
There are two ways to fix this error:
1) If you are using a stateless functional component, change it to a normal React component so that you can use the .children props:
2) Use React. Children. toFragment(props.children) so that you can pass in multiple children: