Hacker News new | ask | show | jobs
by goshakkk 3216 days ago
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.