Multi-step form

Solution retrospective
I am most proud of being able to build this using a new form library I haven't used before, Formik. In the past, I've used React Hook Form but I wanted to try something new.
Next time, I would add type safety to the application by using TypeScript.
What challenges did you encounter, and how did you overcome them?I ran into a bug where the form values setState wasn't working as expected. When a form was submitted it would first call updateFormValues() then updateFormStep(). Here was my original code:
const { formValues, setFormValues } = useContext(FormContext)
const updateFormValues = (newValues) => {
setFormValues({ ...formValues, ...newValues })
}
const updateFormStep = (newStep) => {
setFormValues({ ...prevState, step: newStep })
}
It turns out that updateFormStep()'s setFormValues() was not getting the most up-to-date formValues because React has a tendency of batching state updates togther. As a result, I modified the updateFormStep() function as follows:
const updateFormStep = (newStep) => {
setFormValues((prevState) => ({ ...prevState, step: newStep }))
}
Now the setFormValues() will always provide us with the latest state (prevState) and we use that in updating our state. Meaning, our state management is more robust and predictable.
In essence, I overcame the issue by adding console logs and Googling how react state updates work.
What specific areas of your project would you like help with?Open to any feedback!
Please log in to post a comment
Log in with GitHubCommunity feedback
No feedback yet. Be the first to give feedback on ianwilk20's solution.
Join our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord