When I use useState for a form field I need to update more states to track value, error, overall form. Instead of updating one json object. Idk now that I think about it, maybe it's the same thing. I like hooks more and more but I'm still in transition
This is an excellent use case for useReducer, instead of useState. With that you also make it clearer what state you’re handling, and the actions that can be used to update that state.
I don't know if useReducer would help all that much TBH, but...
You know, the setState() function returned from useState() can be passed a function, to update the state, which receives the current state and you are expected to return the new state.
I feel useReducer() doesn't add all that much to state hooks, other than being familiar to redux users. I share some of the reasons for this point of view in this talk if you are interested: youtube.com/watch?v=gRXgE2iHRek
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
When I use useState for a form field I need to update more states to track value, error, overall form. Instead of updating one json object. Idk now that I think about it, maybe it's the same thing. I like hooks more and more but I'm still in transition
This is an excellent use case for useReducer, instead of useState. With that you also make it clearer what state you’re handling, and the actions that can be used to update that state.
Thanks. I'll look into it
I don't know if
useReducer
would help all that much TBH, but...You know, the
setState()
function returned fromuseState()
can be passed a function, to update the state, which receives the current state and you are expected to return the new state.This can be great when dealing with forms.
Here's that code a little expanded:
I feel
useReducer()
doesn't add all that much to state hooks, other than being familiar to redux users. I share some of the reasons for this point of view in this talk if you are interested:youtube.com/watch?v=gRXgE2iHRek