Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am using Redux. Should I manage controlled input state in the Redux store or use setState at the component level?

I have been trying to figure out the best way to manage my react forms. I have tried to use the onChange to fire an action and update my redux store with my form data. I have also tried creating local state and when my form gets submitted I trigger and action and update the redux store.

How should i manage my controlled input state?

like image 645
gkkirsch Avatar asked Jan 22 '16 17:01

gkkirsch


People also ask

Should I keep all components state in Redux store?

Yes, it's worth striving to store all component state in Redux. If you do, you will benefit from many features of Redux like time travel debugging and replayable bug reports. If you don't, those features could be completely unusable.

What state should I store in Redux?

For the most part if you have a react-redux app I would avoid local state entirely unless you can trying to solve a very component specific problem. For example I would use local state if my component does something on a setInterval and so it is keeping its own timer.

What is the proper way to access Redux store?

It's simple to get access to the store inside a React component – no need to pass the store as a prop or import it, just use the connect function from React Redux, and supply a mapStateToProps function that pulls out the data you need. Then, inside the component, you can pass that data to a function that needs it.

Should I store form data in Redux?

We specifically recommend that most form state probably shouldn't be kept in Redux. However, it's your app, so you should evaluate the specific use cases you need to deal with to determine the right approach.


2 Answers

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.

like image 75
pgsandstrom Avatar answered Oct 04 '22 03:10

pgsandstrom


  1. 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.

  2. 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:

  1. React Forms - It provides a bunch of helper components to make form rendering and validation more simple.

  2. React JSON schema - Allows one to build an HTML form from a JSON schema.

  3. 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:

  1. React Final Form

And one more important contender in the space that's worth checking out is:

  1. Formik

Tried out React Hook Form in my last project - very simple, small footprint and just works:

  1. React Hook Form
like image 30
Dmitry Shvedov Avatar answered Oct 04 '22 03:10

Dmitry Shvedov