Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The State of State Management in React 2017

Jonathan Kemp
September 21, 2017

The State of State Management in React 2017

These days, managing state in web applications is increasingly difficult. Frameworks like Angular, Ember or Aurelia have some mechanism for managing state built in. But what if you're using React? How do you know which state management solution is right for you? In this session, we'll survey the landscape for managing state in React applications, and walk through some of the more popular solutions, such as Redux and MobX, as well as alternatives like Vue, RxJS and Elm.

Jonathan Kemp

September 21, 2017
Tweet

More Decks by Jonathan Kemp

Other Decks in Technology

Transcript

  1. React Components - Class components have the ability to maintain

    internal state. - Functional components do not manage state.
  2. class Clock extends React.Component { constructor(props) { super(props); this.state =

    {date: new Date()}; } render() { return ( <div> <h1>Hello, world!</h1> <h2>It is {this.state.date.toLocaleTimeString()}.</h2> </div> ); } }
  3. Summary of React State - Data used for the visual

    output is stored in state - Unidirectional Data Flow - Performance, Easier to Reason about, Reduce Bugs
  4. Flux Applications have three major parts: 1. the dispatcher 2.

    the stores 3. the views (React components)
  5. Data Flow - Controller-views listen for these events and retrieve

    data from the stores in an event handler. - Views update themselves and all of their descendants.
  6. Benefits of Flux - Makes it easy to reason about

    complex data changes - Opens up possibilities impossible with classic MVC, such as recording and replaying UI state - Easy Testability
  7. Changes in Flux pattern evolution - Have the action creators

    return the dispatched action. - Making the store stateless, a function telling the initial state and how the subsequent actions transform it.
  8. Benefits 1. Stores and actions are just pure functions. 2.

    Hot reloading. 3. Easy transactions (history).
  9. function counter(state = 0, action) { switch (action.type) { case

    'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } let store = createStore(counter); store.subscribe(() => console.log(store.getState()) );
  10. class ObservableTodoStore { @observable todos = []; constructor() { mobx.autorun(()

    => console.log(this.report)); } addTodo(task) { this.todos.push({ task: task, completed: false, assignee: null }); } } const observableTodoStore = new ObservableTodoStore();
  11. @observer class TodoList extends React.Component { render() { const store

    = this.props.store; return ( <ul> {store.todos.map(todo => ( <li key={todo.id}>{todo.text}</li> ))} </ul> ); } } ReactDOM.render(<TodoApp store={ observableTodoStore } />, mountNode);
  12. var app = new Vue({ el: '#app', data: { list:

    [] }, methods: { addTodo: function () { var input = document.getElementById('update'); this.list = this.list.concat(input.value); } } });
  13. Cycle.js Supports - Virtual Dom Rendering - Server Side Rendering

    - JSX, Typescript - React Native - Time Traveling - Routing with the history API - And more
  14. Elm

  15. Features - Functional programming - pure function, immutable data -

    Statically typed - Compiler - descriptively tell what went wrong and in some cases even suggest a solution