Replies: 1 comment 5 replies
-
You shouldn't create a hook in a hook. It can violate the rules of hooks. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We have built a form library (like react hook form but with more features) using Valtio
It is working like a charm, but one challenge I faced was, how to be able to create a hook that would return useSnapshot for one specific field in the state
So, for a little more of context, we create one fieldState with Valtio inside a useRef in a provider, then parse it down, but I need to be able to consume it and return the useSNapshot for that field. We made it and it is working like a charm but, now that we ahve to write some tests, Jest complains that the hook is inside a function:
"Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:"
Can you help me to find out how to deal with it? I tried using watch, subscribe, but I could not succeed, I guess I miss a better understanding overall
This is the code we have now
With this code, any component can use useWatchField('fieldName') and will get updated without any problem.
Beta Was this translation helpful? Give feedback.
All reactions