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

Simplify State Management with Redux 2018

Simplify State Management with Redux 2018

Managing state in web applications these days is increasingly difficult. Redux is a JavaScript framework for managing and maintaining application state that simplifies state management by restricting how data is updated in web apps. These restrictions make it easier to manage data changes and enable powerful developer tools. In this session, we'll look at how data flows in Redux applications and the role functional programming plays by looking at how to build applications with React and Redux. We'll also look at the role developer tools play, as well as how all this can make you a better developer.

Jonathan Kemp

April 20, 2018
Tweet

More Decks by Jonathan Kemp

Other Decks in Technology

Transcript

  1. Redux is... - following certain constraints to create a good

    developer experience. - teaching you programming principles that can make you a better developer.
  2. Features: - Plain Text Calculator - Every calculation gets saved

    to the history - Refer to calculations and reuse them in the calculator
  3. { items: [ { id: 0, expression: '', result: 0

    } ], lastResult: 0, lastExpression: '' }
  4. function calculateApp(state, action) { switch (action.type) { case ADD_ENTRY: return

    Object.assign({}, state, { items: [ { id: state.items.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1, expression: action.text, result: eval(action.text) }, ...state.items ] }) ... } }
  5. function calculateApp(state, action) { switch (action.type) { case REMOVE_ENTRY: return

    Object.assign({}, state, { items: [ ...state.items.slice(0, action.index), ...state.items.slice(action.index + 1) ] }) ... } }
  6. Three Principles of Redux 1. Single source of truth 2.

    State is read-only 3. Changes are made with pure functions
  7. Single source of truth - The state of your whole

    application is stored in an object tree within a single store. - A single state tree also makes it easier to debug or introspect an application.
  8. State is read-only - The only way to mutate the

    state is to emit an action, an object describing what happened. - This ensures that neither the views nor the network callbacks will ever write directly to the state.
  9. Changes are made with pure functions - To specify how

    the state tree is transformed by actions, you write pure reducers.
  10. Pure Functions 1. Given the same input, always returns the

    same output. 2. Do Not Cause Side Effects. 3. Don’t Mutate Arguments.
  11. Pure Functions - Avoid globals - Ensure its data comes

    from inputs and it never reaches outside itself - Don’t modify input - Avoid side-effects
  12. const array1 = [1, 2, 3, 4]; const reducer =

    (accumulator, currentValue) => accumulator + currentValue; // 1 + 2 + 3 + 4 console.log(array1.reduce(reducer)); // expected output: 10 // 5 + 1 + 2 + 3 + 4 console.log(array1.reduce(reducer, 5)); // expected output: 15
  13. 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()) );
  14. 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}
  15. React and Redux - Pure functions, immutable state, reduce, function

    composition, deterministic state - These concepts have carried over into many other libraries and frameworks.
  16. Counter CountIt CountIt const { count } = this.props dispatch({

    type: 'INCREMENT' }) dispatch({ type: 'DECREMENT' }) appReducer Store
  17. Data Flow - call store.dispatch(action). - The Redux store calls

    the reducer function. - The Redux store saves the state tree returned by the root reducer.
  18. Redux Overview - State of your app is in a

    single store. - Only way to change the state tree is to emit an action. - specify how the actions transform the state with pure reducers.
  19. Resources - Getting Started with Redux
 https://egghead.io/courses/getting-started-with-redux - Building Applications

    with Idiomatic Redux
 https://egghead.io/courses/building-react-applications-with-idiomatic-redux
  20. Redux - Helps you write applications that behave consistently -

    Good developer experience - use together with React, or with any other view library. - Teaches concepts important to functional programming
  21. 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();
  22. @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);