Render Props

ref – https://reactjs.org/docs/render-props.html
https://flaviocopes.com/react-render-props/

A common pattern used to share state between components is to use the children prop.

For example, a component’s (this.props.children) references the JSX that you pass into this particular component.

In our case, component Parent’s this.props.children references an array which contains Children1 and Children2 components.

However, there is a problem here: the state of the parent component cannot be accessed from the children.

so we share state like this:

In the Parent, we have a state object with property ‘name’. We pass this into this.props.children
Thus, whatever we pass into JSX Parent component will be able to receive Parent’s state via prop:

However, using the children property on the prop object is very specific. You may not want this.

Render Prop

You have the option to use any prop. We can use someProp1, someProp2 as props in component Parent. It takes in data which we then pass to components Children1 and Children2.

So in component Parent, this.props.someprop1 references function

Then, this.state.name is passed into into this.props.someprop1 in the render function.

This name then goes into the function above and gets passed into component Children1.

Thus, the name ‘Flavio’ is passed into component Children1.

This is called render prop, which refers to a technique for sharing code between React components using a prop whose value is a function.

In other words, a component (Parent) with a render prop (someprop1) references a function:

In our case the function is:

that returns component Children and is called in Parent’s render function:

More concretely, a render prop is a function prop that a component uses to know what to render.