Cannot use hooks because of how containers call their views
See original GitHub issueThe following:
const MyResultView = () => {
const someHook = useHook();
return <div>My Result</div>;
};
<Result view={MyResultView} />
Errors with: https://reactjs.org/warnings/invalid-hook-call-warning.html
I think this is because of how containers call their views:
When I change that to:
return <View
className={className},
result={result}
key={`result-${result.id.raw}`}
onClickLink={() => this.handleClickLink(result.id.raw)}
titleField={titleField}
urlField={urlField}
/>;
Everything works fine.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:16 (12 by maintainers)
Top Results From Across the Web
Invalid hook call. Hooks can only be called inside of the ...
Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: You...
Read more >Understanding common frustrations with React Hooks
React Hooks can be frustrating despite their popularity and widespread use. Learn about some of the drawbacks to using React Hooks.
Read more >Invalid Hook Call Warning
You can only call Hooks while React is rendering a function component : ✓ Call them at the top level in the body...
Read more >What will happen to the Container/Presenter Pattern? - YouTube
React Hooks : What will happen to the Container /Presenter Pattern?
Read more >Best Practices With React Hooks
Don't call Hooks inside loops, conditions, or nested functions. Always use Hooks at the top level of your React function.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Jason, re “I’m hesitant to make the switch from View() to <View /> because I don’t fully understand the functional differences between the two styles.” … the difference is that one is seriously non-idiomatic to React and bypasses React’s internals completely. If you simply call a function you never trigger the tracking that allows React to register and call hooks properly. What’s worse, in certain cases that may not trigger an error, you may actually end up registering the hooks on the wrong component! Bottom line: a function only becomes a component to the React core when you actually render it --as is evidenced by the workaround above, so render it!
https://kentcdodds.com/blog/dont-call-a-react-function-component
Please fix this. It wouldn’t be a minor issue for any React framework
(Elastic is great. Keep up the good work)
For others who run into this, you can workaround via: