rules of hooks question

Im looking at an article rn (https://kentcdodds.com/blog/how-to-use-react-context-effectively) and trying to do something similar in my context with using a custom hook that calls the useContext(). However im getting invalid hook call error, im assuming for abusing the rules of hooks.
10 Replies
bigsexy
bigsexyOP3y ago
that is what my hook looks like pic of error
bigsexy
bigsexyOP3y ago
bigsexy
bigsexyOP3y ago
here a snippet from the article
bigsexy
bigsexyOP3y ago
bigsexy
bigsexyOP3y ago
looking at useCount() in particular is it because im passing in args to the function? im planning on doing something else with the string passed in before the ctx.setSearchQuery is called within my hook is the reason im using a hook.
shaaah
shaaah3y ago
useCount here is a function and not a component, Hooks can only be called inside a component that too top level, it cant even be called inside useEffect, functions etc So you need to put your React.useContext inside a component and call it from there
Develliot
Develliot3y ago
If you are making a custom hook, naming is super important. It needs to have the prefix 'use' as in 'useBlahBlah' tell react this is a hook, treat it as such".
bigsexy
bigsexyOP3y ago
yea i took off use because too check if it still error'ed out or if it was just the ts rule afaik react doesnt actually care if you use use right? its just for liniting
Develliot
Develliot3y ago
Ah you are quite right I thought it was implicit behavior, How have you set up the context?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server