Let’s have a look at the React Function as Children Pattern.
The simple component
Usually, JSX/TSX code looks like this:
1 | <SomeComponent> |
The inline JS/TS code
One complication is rending JSX/TSX inline conditionally:
1 | <SomeComponent> |
The code in curly braces is plain JS/TS, so it’s JS/TS wrapped in JSX/TSX, which is again wrapped in JS/TS. 🤯
The anonymous function with magically appearing props
So far, so good. But some time ago I stumbled upon this syntax in Formik, a form library for React, which I found even more confusing at first glance:
1 | <Formik> |
What’s that, a function call inside JSX/TSX? Where do values
and handleChange
come from so magically? 🤯🤯🤯
That’s the so-called “function as children pattern”: The Formik
component takes a function as a child, which is called with some arguments. The function returns JSX/TSX.
Unwrapping the magic
Let’s unwrap this with a more simple example:
1 | type Props = { |
The MyComponent
component takes a function as a child, which is called with no arguments. The function returns JSX/TSX. So why would ony use a function instead of a plain JSX/TSX element? The answer is: The function can be called with arguments, which can be passed from the parent component. This is a powerful pattern to pass data and state from the parent to the child component:
1 | <MyComponent> |
This is shorthand syntax for:
1 | <MyComponent> |
Or to make it even more clear, here the syntax with function
style:
1 | <MyComponent> |
Now it’s even readable for the old school JS developers like me. 🤓
Passing either plain JSX/TSX or a function
It’s also possible to pass either a function or a JSX/TSX element to the children
prop:
1 | type Props = { |
That’s also what Formik actually does.
Like what you read?
You can hire me or make a donation via PayPal!