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

The State of State Management in React 2018

The State of State Management in React 2018

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

March 30, 2018
Tweet

More Decks by Jonathan Kemp

Other Decks in Technology

Transcript

  1. Overview - What is State? - Managing State with React,

    Redux, and Mobx - Context - Alternatives
  2. React Components - Class components have the ability to maintain

    internal state. - Functional components do not manage state.
  3. 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> ); } }
  4. class TodoApp extends React.Component { constructor(props) { super(props); ... this.state

    = { items: [], text: '' }; } onChange(e) { this.setState({text: e.target.value}); }
  5. Counter CountIt CountIt this.state = { count: 0 }; this.setState((prevState)

    => ({ count: prevState.count + 1 })); this.setState((prevState) => ({ count: prevState.count - 1 }));
  6. Summary of React State - Data used for the visual

    output is stored in state - Unidirectional Data Flow - Performance, Easier to Reason about, Reduce Bugs
  7. 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
  8. Three Principles of Redux 1. Single source of truth 2.

    State is read-only 3. Changes are made with pure functions
  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. export default theDefaultReducer = (state = 0, action) => state;

    export const firstNamedReducer = (state = 1, action) => state; export const secondNamedReducer = (state = 2, action) => state; const rootReducer = combineReducers({ theDefaultReducer, firstNamedReducer, secondNamedReducer }); const store = createStore(rootReducer); console.log(store.getState()); // {theDefaultReducer : 0, firstNamedReducer : 1, secondNamedReducer : 2}
  11. Counter CountIt CountIt const { count } = this.props dispatch({

    type: 'INCREMENT' }) dispatch({ type: 'DECREMENT' }) appReducer Store
  12. 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();
  13. @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);
  14. class ObservableTodoStore { @observable todos = []; constructor() { mobx.autorun(()

    => console.log(this.report)); } @action.bound addTodo(task) { this.todos.push({ task: task, completed: false, assignee: null }); } } const observableTodoStore = new ObservableTodoStore();
  15. More on Actions - Actions should always be used on

    functions that modify state. - Using actions yields performance benefits - action provides useful debugging information with devtools. - Strict mode enforces all state modifications are done by an action.
  16. Disclaimer - It is an experimental API likely to break

    in future releases of React. - The vast majority of applications do not need to use context. - It is far more likely that Redux or Mobx is the right solution. - If you aren’t an experienced React developer, don’t use context.
  17. var app = new Vue({ el: '#app', data: { list:

    [] }, methods: { addTodo: function () { var input = document.getElementById('update'); this.list = this.list.concat(input.value); } } });
  18. Elm

  19. Features - Functional programming - pure functions, immutable data -

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