Fireship · @fireship

Redux in 100 Seconds

Uploaded April 21, 2022 · Length 2:33

Redux in 100 Seconds

Summary

redux pattern and library for state management in react applications

  • redux
  • react state management
  • single source of truth
  • immutable state
  • react hooks

Transcript preview

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

  1. 0:00

    redux a single source of truth for all

  2. 0:02

    the data in your javascript application

  3. 0:04

    modern web applications are represented

  4. 0:07

    as a complex tree of components

  5. 0:09

    components that constantly produce and

  6. 0:11

    share data called state and when state

  7. 0:12

    is decentralized it can become difficult

  8. 0:14

    to understand and test redux is both a

  9. 0:17

    pattern and library that helps

  10. 0:18

    developers implement complex state

  11. 0:20

    management requirements at scale it was

  12. 0:22

    created by dan abramov and andrew clark

  13. 0:25

    at facebook and is one of the most loved

  14. 0:27

    and hated libraries in the react

  15. 0:28

    ecosystem it relies on a single

  16. 0:30

    immutable object to store all of the

  17. 0:32

    application state kind of like a

  18. 0:34

    client-side database to change the state

  19. 0:36

    like when a button is clicked an action

  20. 0:38

    will be dispatched which has a name like

  21. 0:40

    an event and a payload with the data

  22. 0:42

    that it wants to change remember the

  23. 0:43

    store is immutable so to change the

  24. 0:45

    state of the application an entirely new

  25. 0:47

    object is created by passing the current

  26. 0:49

    state and the action payload into a

  27. 0:51

    reducer function which returns a new

  28. 0:53

    object with the entire application state

  29. 0:56

    the end result is a one-way data flow

  30. 0:58

    that is predictable and testable and it

  31. 1:00

    also opens the door to awesome dev tools

  32. 1:02

    that allow you to time travel through

  33. 1:03

    your application's data but it comes at

  34. 1:05

    a cost of additional boilerplate code

  35. 1:07

    that might add too much complexity to a

  36. 1:09

    smaller project to get started create a

  37. 1:11

    react app with your favorite tool then

  38. 1:13

    install the redux toolkit first use

  39. 1:15

    configure store to set up the global

  40. 1:17

    store object which will register any

  41. 1:19

    reducers defined elsewhere in the code

  42. 1:21

    then provider will make its data

  43. 1:23

    accessible to the entire component tree

  44. 1:25

    next create a slice like pizza to

  45. 1:27

    represent some data in the store it

  46. 1:29

    should have a unique name and initial

  47. 1:30

    state but most importantly it contains a

  48. 1:33

    collection of reducers which are

  49. 1:34

    functions that take the old state and an

  50. 1:37

    action then define the logic required to

  51. 1:39

    change the state redux toolkit will

  52. 1:41

    automatically generate actions that

  53. 1:43

    correspond to the names of these reducer

  54. 1:45

    functions we can export them then put

  55. 1:47

    them to use in an actual ui component

  56. 1:49

    the beauty of redux is that we can

  57. 1:51

    select data anywhere in the application

  58. 1:53

    without the need for context or prop

  59. 1:55

    drilling instead we can grab any

  60. 1:57

    reactive value or slice in the store

  61. 1:59

    with the use selector hook now to change