Taken from Rules of Hooks:
Don’t call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function
Given this recommendation by the react team, would using a hook at the top of a render props function be inadvisable? Assuming it depends on the value returned by the function.
const MyComponent = () => (
<RenderPropComponent>
{value => {
React.useEffect(() => {
// Magic
}, [value]);
return <p>Hello</p>;
}}
</RenderPropComponent>
);
I don't feel like it breaks their requirement
By following this rule, you ensure that Hooks are called in the same order each time a component renders
But should I be doing the following instead?
const MyComponent = ({ value }) => {
React.useEffect(() => {
// Magic
}, [value]);
return <p>Hello</p>;
};
const MyContainer = () => (
<RenderPropComponent>
{value => <MyComponent value={value} />}
</RenderPropComponent>
);
React.createElement('MyComponent', {value}, null)part. I'm not saying this won't work, but option A looks odd to me.useEffectas a nested function ofRenderPropComponentand thus should be avoided.RenderPropComponentcould callchildren(value)conditionally.