What to consider in integrating react-hook-form with zustand. #1922
-
When I embarked on the development of my app, I initially prioritized simplicity over architectural design. However, as my app grew more complex and I aimed to implement validation using the powerful react-hook-form library, I found myself grappling with the challenge of integrating it seamlessly. Given that react-hook-forms already handles form data for me, and all my form data currently relies on zustand, I'm in need of guidance on the best approach to integrate the two. At present, I utilize a slice similar to the following code snippet to manage form data: export const createDataFormSlice = (set) => ({
dataForm: {},
updatePrimitive: (key, value) => set(state => ({
dataForm: {
...state.dataForm,
[key]: value
}
})),
resetDataForm: () => set({ dataForm: {} })
}) For each input field, I attach an event listener like onChange, which updates the dataForm accordingly. I am beginning to question whether my usage of zustand has been flawed all along, perhaps due to my limited knowledge and hasty decision-making. I would be immensely grateful for any guidance, suggestions, or insights you can offer. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
I think this question is pointless |
Beta Was this translation helpful? Give feedback.
-
So, this is actually a good question and I'm sorry no one picked it up properly. In the old days when redux was overused and the only real option, there were other popular libraries to handle forms such as formik and redux-form and the first was more popular of the latter exactly because of the issue dbritto is talking about (see here https://formik.org/docs/overview#why-not-redux-form). @dbritto-dev is the correct answer but it might be too simplistic and not really pragmatic related to certain organizations we're required to work in. Given that your best choice is to store the data collected in each step of the wizard in the server, and then hydrate the form when you visit each step by calling something like form.reset, we have to acknowledge that this might not be always possible for whatever reason (backend devs too busy to implement the intermediate calls, company not willing to pollute db until the wizard is completed and any other reason that is technically solvable but maybe outside of your impact area). Exactly because of this, a similar solution to the one you're talking about was proposed here: /~https://github.com/orgs/react-hook-form/discussions/6382. So what to do if you have a wizard of, let's say, 3 steps and all the data gathered in each page must be committed to the server at the end of the flow and you also have to hydrate the form once you visit the steps after a user committed the data to the server already? I have a few tips:
NB: Keep in mind that rhf isn't really meant to be used like this mainly because uses uncontrolled components where instead this situation requires components to be controlled from an outside store. This is also the reason why you don't want to change an external store like zustand each time that there is an onChange event on one of your fields. Still, I think the above solution is an acceptable tradeoff and using rhf is, imho, the preferable solution, but it might not be for everyone! |
Beta Was this translation helpful? Give feedback.
@ahmadxgani I would leave
RHF
as is. Having to sync that data with zustand and viceversa is not a good idea.