I am using Redux. Should I manage controlled input state in the Redux store or use setState at the component level?
I like this answer from one of the Redux co-authors: https://github.com/reactjs/redux/issues/1287
Use React for ephemeral state that doesn't matter to the app globally and doesn't mutate in complex ways. For example, a toggle in some UI element, a form input state. Use Redux for state that matters globally or is mutated in complex ways. For example, cached users, or a post draft.
Sometimes you'll want to move from Redux state to React state (when storing something in Redux gets awkward) or the other way around (when more components need to have access to some state that used to be local).
The rule of thumb is: do whatever is less awkward.
That is, if you're sure that your form won't affect global state or need to be kept after your component is unmounted, then keep in the react state.
You can use the component's own state. And then take that state and give it as an argument to the action. That's pretty much the "React way" as described in the React Docs.
You can also check out Redux Form. It does basically what you described and links the form inputs with Redux State.
The first way basically implies that you're doing everything manually - maximum control and maximum boilerplate. The second way means that you're letting the higher order component do all the work for you. And then there is everything in between. There are multiple packages that I've seen that simplify a specific aspect of form management:
React Forms - It provides a bunch of helper components to make form rendering and validation more simple.
React JSON schema - Allows one to build an HTML form from a JSON schema.
Formsy React - As the description says: "This extension to React JS aims to be that "sweet spot" between flexibility and reusability."
Update: seems these days Redux Form is being replaced with:
- React Final Form
And one more important contender in the space that's worth checking out is:
- Formik
Tried out React Hook Form in my last project - very simple, small footprint and just works:
- React Hook Form
TL;DR
It's fine to use whatever as it seems fit to your app (Source: Redux docs)
Some common rules of thumb for determing what kind of data should be put into Redux:
- Do other parts of the application care about this data?
- Do you need to be able to create further derived data based on this original data?
- Is the same data being used to drive multiple components?
- Is there value to you in being able to restore this state to a given point in time (ie, time travel debugging)?
- Do you want to cache the data (ie, use what's in state if it's already there instead of re-requesting it)?
These questions can easily help you identify the approach that would be a better fit for your app. Here are my views and approaches I use in my apps (for forms):
Local state
- Useful when my form has no relation to other components of the UI. Just capture data from
input
(s) and submits. I use this most of the time for simple forms. - I don't see much use case in time-travel debugging the input flow of my form (unless some other UI component is dependent on this).
Redux state
- Useful when the form has to update some other UI component in my app (much like two-way binding).
- I use this when my form
input
(s) causes some other components torender
depending on what is being input by the user.