React Children
This tutorial experiments with the numerous ways we can provide (and misuse) the children
prop in React.
Contents
- Sandboxes
- Template
- Pass
children
as a single string (third argument) - Pass
children
as several strings (function overloading) - Pass
children
pass in a single array - Pass
children
pass in nested arrays - Pass
children
pass in a single array - Pass
children
as a named prop - Intercept
children
in React Components - Count children with
React.Children.count
- Iterate over children with
Children.map
- Iterate and insert children with
Children.forEach
- Ensure there is only one child with
Children.only
- Convert children to array with
Children.toArray
Sandboxes
Template
Pass children
as a single string (third argument)
Pass children
as several strings (function overloading)
Pass children
pass in a single array
Pass children
pass in nested arrays
Pass children
pass in a single array
Objects (and related complex types like Date
) will result in an error.
Pass children
as a named prop
Intercept children
in React Components
Count children with React.Children.count
Iterate over children with Children.map
Iterate and insert children with Children.forEach
Ensure there is only one child with Children.only
Convert children to array with Children.toArray
Use this to manipulate the array.
(filter
, sort
, reverse
, etc.)