Children as Function
In React, you can pass a function as children
to a component:
This technique is commonly referred to as render callbacks
, and is used by many different libraries such as React Router and React Motion. When applied, rendering logic can be kept in the owner component, instead of being delegated.
Here’s an example where we’ve created a component that automatically measures the size of it’s parent container and passes those parameters down to it’s children.
This prop is called the “render callback”. That function is able to receive parameters that will be assigned by the child the moment it is called, with the information the child has when doing its stuff. The child is the one to decide if it wants to call that function or not, again depending on its internal logic.
How it works Let’s look at how we might implement our previous example in a fairly naive way:
When to use this pattern
Allows you to mix-and-match components
Which enables more modular (maintainable) code
As an alternative to
context
Caveats Because of the no-inline-lambdas
ESLint rule, children-as-function currently need to be written as such:
The children
prop is commonly renamed, typically to render
, so don’t be alarmed if you see a component using this naming convention.
Resources
Video Lecture https://courses.reacttraining.com/courses/advanced-react/lectures/3066347
Last updated