Even using the term "higher order" that seems like a very OOP solution, while render props is very functional and thus a better idiom for JavaScript.
"Higher order" sounds pretty functional to me. Higher order functions and all. Although I guess the term higher order function is hardly even spoken of in real functional languages because they're taken for granted.
Use a Render Prop
11–20 of 35 posts
Re: Use a Render Prop
#12Then, the final example looks like:
{({ x, y }) => (
The mouse position is ({x}, {y})
)}
[0]: https://discuss.reactjs.org/t/children-as-a-function-render-...Re: Use a Render Prop
#13As a note, it is not safe for a component using a render prop to implement shouldComponentUpdate. The problem is that the render prop may close over data that the component is unaware of. The Asana core components used to the use the render prop pattern until we discovered that issue and also realized it made tests harder to write. Now we use React.cloneElement instead.
Re: Use a Render Prop
#14As a note, it is not safe for a component using a render prop to implement shouldComponentUpdate. The problem is that the render prop may close over data that the component is unaware of. The Asana core components used to the use the render prop pattern until we discovered that issue and also realized it made tests harder to write. Now we use React.cloneElement instead.
Re: Use a Render Prop
#15As a note, it is not safe for a component using a render prop to implement shouldComponentUpdate. The problem is that the render prop may close over data that the component is unaware of. The Asana core components used to the use the render prop pattern until we discovered that issue and also realized it made tests harder to write. Now we use React.cloneElement instead.
Would you mind elaborating on how React.cloneElement works vs this?
I came up with the pattern after discovering the issues with render callbacks and before Higher Order Components were common. You can achieve the same result with a Higher Order Component. The only benefit of this approach over an HOC is that you write a regular Component.
To implement the pattern, you make you Component expect a single child for it's `children` prop. Let's assume that the child has a `data` prop which is not set in the parent Component. In render, you return `React.cloneElement(this.props.children, { data: this.state.valueToInject })`.
The value of a Higher Order Component is that you can usually define a map to props function.
Re: Use a Render Prop
#16Earlier quoted context omitted.
Would you mind elaborating on how React.cloneElement works vs this?
_On mobile so not a fully detailed response_ I came up with the pattern after discovering the issues with render callbacks and before Higher Order Components were common. You can achieve the same result with a Higher Order Component. The only benefit of this approach over an HOC is that you write a regular Component. To implement the pattern, you make you Component expect a single child for it's `children` prop. Let'…
Re: Use a Render Prop
#17Earlier quoted context omitted.
"Higher order" sounds pretty functional to me. Higher order functions and all. Although I guess the term higher order function is hardly even spoken of in real functional languages because they're taken for granted.
The term “higher order” generally indicates complexity and indirection. If you can solve something first order, then that is much more preferable to a higher order (function, logic, object) solution. Higher order functions fall into the same category (you can have them if you want, but you must understand what you are getting into in terms of complexity).
As do all abstractions.
> If you can solve something first order, then that is much more preferable to a higher order solution.
a.k.a. "To abstract, or not to abstract?". It really depends, of course. But I know for certain I would not want to solve "mapping over an array" without the Array.prototype.map higher-order function.
Re: Use a Render Prop
#18This technique is very useful, but passing the callback as a prop is an ugly way to do it. Much cleaner to pass the callback as children [0]. Then, the final example looks like: {({ x, y }) => ( The mouse position is ({x}, {y}) )} [0]: https://discuss.reactjs.org/t/children-as-a-function-render-...
I've literally had good developers not understand them until I switched an example from using children to using a render prop, at which point there's a big light bulb moment.
So i'll be sticking with the render prop.
Re: Use a Render Prop
#19Re: Use a Render Prop
#20People seem to forget that components (stateless components, stateless functional components or whatever you want to call them) are just functions. One function calls another function, that's function composition. In plain JavaScript we do it all the time. But when React is involved people are suddenly all like: no you can't call that function directly, you have to name it 'Component' with a capital C and use like '…