One-way direction data flow

One-way direction data flow is a pattern that works nicely with React. It is around the idea that the components do not modify the data that they receive. They only listen for changes of this data and maybe provide the new value but they do not update the actual data. This update happens following another mechanism in another place and the component just gets re-rendered with the new value.

Let’s for example get a simple Switcher component that contains a button. If we click it we have to enable a flag in the system.

  1. class Switcher extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = { flag: false };
  5. this._onButtonClick = e => this.setState({
  6. flag: !this.state.flag
  7. });
  8. }
  9. render() {
  10. return (
  11. <button onClick={ this._onButtonClick }>
  12. { this.state.flag ? 'lights on' : 'lights off' }
  13. </button>
  14. );
  15. }
  16. };
  17. // ... and we render it
  18. function App() {
  19. return <Switcher />;
  20. };

At this moment we have the data inside our component. Or in other words, Switcher is the only one place that knows about our flag. Let’s send it out to some kind of a store:

  1. var Store = {
  2. _flag: false,
  3. set: function(value) {
  4. this._flag = value;
  5. },
  6. get: function() {
  7. return this._flag;
  8. }
  9. };
  10. class Switcher extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = { flag: false };
  14. this._onButtonClick = e => {
  15. this.setState({ flag: !this.state.flag }, () => {
  16. this.props.onChange(this.state.flag);
  17. });
  18. }
  19. }
  20. render() {
  21. return (
  22. <button onClick={ this._onButtonClick }>
  23. { this.state.flag ? 'lights on' : 'lights off' }
  24. </button>
  25. );
  26. }
  27. };
  28. function App() {
  29. return <Switcher onChange={ Store.set.bind(Store) } />;
  30. };

Our Store object is a singleton where we have helpers for setting and getting the value of the _flag property. By passing the setter to the Switcher we are able to update the data externally. More or less our application workflow looks like that:

one-direction data flow

Let’s assume that we are saving the flag value to a back-end service via the Store. When the user comes back we have to set a proper initial state. If the user left the flag as true we have to show “lights on” and not the default “lights off”. Now it gets tricky because we have the data in two places. The UI and the Store have their own states. We have to communicate in both directions from the store to the switcher and from the switcher to the store.

  1. // ... in App component
  2. <Switcher
  3. value={ Store.get() }
  4. onChange={ Store.set.bind(Store) } />
  5. // ... in Switcher component
  6. constructor(props) {
  7. super(props);
  8. this.state = { flag: this.props.value };
  9. ...

Our workflow changes to the following:

one-direction data flow

All this leads to managing two states instead of one. What if the Store changes its value based on other actions in the system. We have to propagate that change to the Switcher and we increase the complexity of our app.

One-way direction data flow solves this problem. It eliminates the multiple places where we manage states and deals with only one which is usually the store. To achieve that we have to tweak our Store object a little bit. We need logic that allows us to subscribe for changes:

  1. var Store = {
  2. _handlers: [],
  3. _flag: '',
  4. subscribe: function(handler) {
  5. this._handlers.push(handler);
  6. },
  7. set: function(value) {
  8. this._flag = value;
  9. this._handlers.forEach(handler => handler(value))
  10. },
  11. get: function() {
  12. return this._flag;
  13. }
  14. };

Then we will hook our main App component and we’ll re-render it every time when the Store changes its value:

  1. class App extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = { value: Store.get() };
  5. Store.subscribe(value => this.setState({ value }));
  6. }
  7. render() {
  8. return (
  9. <div>
  10. <Switcher
  11. value={ this.state.value }
  12. onChange={ Store.set.bind(Store) } />
  13. </div>
  14. );
  15. }
  16. };

Because of this change the Switcher becomes really simple. We don’t need the internal state and the component may be written as a stateless function.

  1. function Switcher({ value, onChange }) {
  2. return (
  3. <button onClick={ e => onChange(!value) }>
  4. { value ? 'lights on' : 'lights off' }
  5. </button>
  6. );
  7. };
  8. <Switcher
  9. value={ Store.get() }
  10. onChange={ Store.set.bind(Store) } />

Final thoughts

The benefit that comes with this pattern is that our components become dummy representation of the store’s data. There is only one source of truth and this makes the development easier. If you are going to take one thing from this book, I would prefer it to be this chapter. The one-direction data flow drastically changed how I think when designing a feature so I believe it will have the same effect on you.