How to Properly Sync Zustand and React Hook Form Data After Page Refresh? #12412
Unanswered
frontendjulien
asked this question in
Q&A
Replies: 2 comments
-
Can you provide a CodeSandbox link with a reproduction? Can you log this line during the initial render and see what values are being stored? const { name, email, address } = useFormStore((state) => state.form); Also, what does |
Beta Was this translation helpful? Give feedback.
0 replies
-
looking for solution too. i have an identical problem with shadcn form, useForm with zod, separate component with input, and zustand. the only solution i found is to redirect user to page where he can return back with regular anchor, therefore to reset page |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello guys, I'm new to working with React Hook Form and Zod. I'm building a form with Shadcn, React Hook Form, and Zod, and I'm implementing Zustand to persist data across page refreshes.
The issue I'm running into is that after a page refresh or when a user navigates back to the form, the input data persists visually as expected, but React Hook Form throws validation errors on submit, as if the inputs were empty. From what I understand, It seems like React Hook Form doesn’t really sync with Zustand's persisted data, which causes validation to fail.
Zustand Form Store
Input Field Component
I've found a workaround that involves using React Hook Form’s reset function in a useEffect to update the states to match Zustand's values when the component mounts. While this works, it may not be the best solution/optimal way because I have many forms with 10+ fields. So adding useEffect in every form would look very messy and not ideal.
If anyone else encountered this issue or know a cleaner/better solution, please please share your knowledge. I'd really appreciate any guidance. Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions