Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Redux unidirectional?

After implementing Flux, it was easy to see that it is in fact unidirectional.

Flux recap: The View takes in user actions, which get sent to and interpreted by Action Creators, which get sent to the Dispatcher, which get broadcast to the Stores, which eventually trigger an update back in the View.

With Redux, although I've only read a handful of blog posts on it, it seems as though the Action Creators are in fact returning the formatted action object back to the View that fired off the action in the first place.

See: https://code-cartoons.com/a-cartoon-intro-to-redux-3afb775501a6#.3hquuzljm

So the question is this: Does having the Action Creator by design return to the View disrupt the very notion of uni-directional workflow?

I haven't implemented Redux just yet, so perhaps I'm missing a step, but this sticks out to me as a big difference between Flux and Redux.

like image 313
Danny Delott Avatar asked Jun 10 '26 23:06

Danny Delott


1 Answers

I think you're putting too much emphasis on what action creators are.

An action creator is literally just this:

function login(username, password) {
  return { type: 'LOGIN', payload: { username, password };
}

These two things are identical:

class Component extends React.Component {
  ...
  render() {
    const { username, password } = this.props;

    return (
      ...
        dispatch({ type: 'LOGIN', payload: { username, password });
        // or
        dispatch(login(username, password));
      ...
    );
  }
}

And then if you user react-redux's connect, it can look like

class Component extends React.Component {
  ...
  render() {
    const { username, password } = this.props;

    return (
      ...
        login(username, password)); // connect binds the AC to dispatch for convenience.
      ...
    );
  }
}

Action creators are not a large abstraction. they're just a special name for a function that helps makes actions to keep your code DRY. They don't do anything to change the unidirectional model.

like image 58
Nathan Hagen Avatar answered Jun 12 '26 12:06

Nathan Hagen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!