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

Global state management

Global state management

In this presentation, we explore the state management problem that we have using only local state, how this problem can be solved by using global state management library (Redux & Rematch), as well some of the important concepts from functional programming crucial for understanding and using Redux.

Konstantin

March 27, 2019
Tweet

More Decks by Konstantin

Other Decks in Programming

Transcript

  1. !3

  2. !4

  3. !5

  4. !6

  5. !7

  6. !8

  7. WHAT TO PUT INTO GLOBAL STATE DO PUT ✅ Session

    data ✅ Same data that has different representation in different components ✅ Data that you want to cache / make available offline DON'T PUT ❌ UI component state (is dropdown open?) ❌ Loading state (isLoading, isCreating, isUpdating, etc..) ❌ Form values Use these as guidelines, not as rules. !11
  8. WHAT IS IMMUTABILITY? • Immutable variable is a variable that

    once initialized, cannot be changed • To update its value, we create a new variable !13
  9. WHY IMMUTABILITY? • Makes your code predictable • Easier to

    test • Helps with render optimization !14
  10. PURE FUNCTIONS • Given the same input, always return the

    same output • Don’t change their arguments • Have no side-effects (don’t reference any external resources - API, localStorage, etc.) !16
  11. WHAT’S REDUX? • Predictable state container • Based on 3

    main principles (3 pillars of Redux) !24
  12. DESIGNING STATE SHAPE • Keep it as flat as possible

    • Have top-level properties as “models” • Deduplicate/normalize data • Think of global state as client-side DB !29
  13. SUMMARY • State management problem • Global state vs. local

    • Immutability and purity • Redux !35