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

What the -dux?

What the -dux?

A introduction to Redux internals, patterns, and best practices.

Ari Rizzitano

April 07, 2016
Tweet

More Decks by Ari Rizzitano

Other Decks in Programming

Transcript

  1. “State”? Wut? • Application data stored in browser memory •

    May be persisted server-side • Can be simple (“isMenuOpen”) or complex (“visualizationTimeseries”)
  2. Redux • Lightweight, functional, framework-agnostic JS state manager • Similar

    to Flux, inspired by Elm patterns • Easily debuggable and testable
  3. Three Principles • Single source of truth • State is

    read-only • Changes are made with pure functions
  4. Action • Role of a message or event • Required

    field: type • Created by action creators • Consumed by reducers
  5. Action { type: ADD_TODO, text: ‘Feed the cat’ }; {

    type: TOGGLE_MENU, open: false };
  6. Action Creator • Function that returns an action • Invoked

    inside dispatch calls • This is where actions are defined
  7. dispatch • The only public way to change the store

    • Invokes action creators • Broadcasts actions so they can be consumed by reducers
  8. Reducer • Receives all actions, but only consumes some •

    Uses actions’ type to find the ones it wants • May mutate the data it receives from actions • Returns a chunk of the store (subtree)
  9. Reducer import { ADD_TODO } from ‘./ActionCreators’; function todoList(state=[], action)

    { switch (action.type) { case ADD_TODO: return [ …state, action.todoText.trim() ]; default: return state; } }
  10. Store • Holds a single, read-only global state tree •

    Can ONLY be modified by reducers. You cannot modify it directly.
  11. Metaphor Caveats • dispatch delivers ALL actions to ALL reducers

    — the reducers decide which actions are theirs. • The store holds the return values of ALL reducers • Reducers are bundled together into one RootReducer
  12. connect() • Provides interface between React and Redux • Allows

    a component to read state and call dispatch
  13. connect() @connect(mapStateToProps, mapDispatchToProps) export default class TodoList extends React.Component {

    // . . . } function mapStateToProps(state) { // . . . } function mapDispatchToProps(dispatch) { // . . . }
  14. mapStateToProps @connect(mapStateToProps, mapDispatchToProps) export default class TodoList extends React.Component {

    render() { // todoList is now available as a prop! let todoList = this.props.todoList; } } function mapStateToProps(state) { return { todoList: state.todoList }; }
  15. mapDispatchToProps import { addTodo } from ‘./ActionCreators’; function mapDispatchToProps(dispatch) {

    return { onTodoAdd: todoText => dispatch( addTodo(todoText) ), onTodoComplete: todoIndex => dispatch( completeTodo(todoIndex) ) }; }
  16. mapDispatchToProps @connect(mapStateToProps, mapDispatchToProps) export default class TodoList extends React.Component {

    handleAddButtonClick(e) { this.props.onTodoAdd(this.state.newTodoText); } } function mapDispatchToProps(dispatch) { return { onTodoAdd: . . ., onTodoComplete: . . . }; }
  17. Thunk • Action creator that returns a function • Can

    call dispatch to fire another action creator • Good for async stuff (e.g. calling dispatch after an XHR response)
  18. Testing • Redux functions are pure • Return value only

    determined by input values • No side effects, no dependencies, no mocks • No excuse not to test!
  19. Testing • Thunks do have side effects, so dispatch and

    store need to be mocked • Use a library (redux-mock-store) or DIY • No need to test dispatch or store
  20. Testing • Redux code can be tested separately from React

    • Redux recommends Mocha • Avoid plugins/helpers if possible • https://github.com/gaearon/redux-devtools
  21. Fin