
Should you store your form state in Redux? - davedx
https://goshakkk.name/should-i-put-form-state-into-redux/
======
davedx
After a few projects and lots of trying out different ways of doing this, I
think I agree with the article. It's not always easy to know in advance which
of the points in the article's checklists are ticked though... requirements
change, apps evolve, and I've found it can be painful to migrate things stored
in React state to Redux after the fact.

Very interested to hear what others think about this.

~~~
goshakkk
You can't always know in advance, and that's perfectly cool. The bigger point
I'm making is, put things into Redux only _once they become global_.

It could be argued that migrating to Redux can be non-trivial, but there are
certain things you can do to ease the potential transition.

In particular, there is nothing stopping you from applying the Redux approach
on the scale of just one component, by not mutating the state in the component
directly, but instead by creating small "action reducer" functions which
return the new state. Kinda similar to what ReasonML with React tries to do.

Roughly something along the lines of:

    
    
        function changeEmail(state, event) {
          return { ...state, email: event.target.value };
        }
    
        class FormComponent extends Component {
          // ...
          handleEmailChange = (evt) => {
            this.setState(state => changeEmail(state, evt));
          };
        }
    

Afterwards, it's easy to migrate this to Redux:

1\. Instead of `setState(state => changeEmail(...))` you would dispatch an
action.

2\. Instead of getting the current value from this.state you would get it from
this.props.

3\. The `changeEmail` function can trivially become a branch in the form's
reducer function.

