Fireship · @fireship

Redux from Scratch

Uploaded June 27, 2018 · Length 9:46

Redux from Scratch

Summary

build custom redux state management library with rxjs and angular

  • redux from scratch
  • custom state management
  • rxjs angular
  • angular state management

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:06

    why use a state management library and

  3. 0:08

    angular when you can just build your own

  4. 0:10

    from scratch when you combine rxjs with

  5. 0:12

    angular's dependency injection it

  6. 0:14

    provides a powerful foundation for

  7. 0:16

    building your own state management

  8. 0:17

    system that means you can get all the

  9. 0:19

    benefits of the redux pattern including

  10. 0:20

    the dev tools that you see here on the

  11. 0:22

    right but you can create your own

  12. 0:23

    abstractions and conventions to reduce

  13. 0:25

    the amount of boilerplate code plus

  14. 0:27

    you'll have a much better understanding

  15. 0:28

    of how your app works under the hood

  16. 0:30

    over the next few minutes you'll learn

  17. 0:31

    how to use custom rxjs operators and

  18. 0:33

    multi casting to build your own state

  19. 0:36

    management library but mostly I just

  20. 0:37

    want to show you how this pattern works

  21. 0:39

    at a fundamental level if you're new

  22. 0:41

    like and subscribe and grab the source

  23. 0:42

    code from angular firebase comm in case

  24. 0:45

    you're not familiar with the redux

  25. 0:46

    pattern here's a quick refresher the

  26. 0:48

    whole idea is that we treat our app data

  27. 0:50

    or state as a global immutable object

  28. 0:53

    the only way we can change the current

  29. 0:54

    state of the app is by dispatching an

  30. 0:57

    action so that might happen on a button

  31. 0:59

    click then the payload for that action

  32. 1:00

    will go to a reducer function to

  33. 1:02

    determine what the next state should

  34. 1:04

    look like the store itself isn't

  35. 1:06

    observable so everything will happen in

  36. 1:08

    a reactive way so anything that

  37. 1:10

    subscribes to the store will be updated

  38. 1:12

    with the latest data the benefit of this

  39. 1:14

    is that we now have a one-way data flow

  40. 1:16

    that provides a predictable history of

  41. 1:18

    all the changes that occur in the app so

  42. 1:19

    we can do time-travel debugging and cool

  43. 1:21

    stuff like that if we think about this

  44. 1:24

    in code our current state is equal to

  45. 1:26

    the stream of actions and the function

  46. 1:28

    that reduces them a reducer is just a

  47. 1:30

    pure function that takes in the current

  48. 1:32

    state and the action and then returns

  49. 1:34

    the next state so let's go ahead and

  50. 1:36

    build this thing the first thing I want

  51. 1:37

    to do is generate a service called store

  52. 1:39

    and I'd also like to point out that I

  53. 1:41

    have lodash installed not only will it

  54. 1:43

    dramatically simplify our code but it

  55. 1:45

    will also give us better performance

  56. 1:47

    than we'd be able to achieve natively so

  57. 1:49

    first I want to show you how our store

  58. 1:51

    service will be used in a component one

  59. 1:53

    really important concept in redux is

  60. 1:54

    that our components should be stateless

  61. 1:56

    meaning they should only be listening to

  62. 1:58

    data from the store as an observable or