Fireship · @fireship
Redux in 100 Seconds
Uploaded April 21, 2022 · Length 2:33
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.
- 0:00
redux a single source of truth for all
- 0:02
the data in your javascript application
- 0:04
modern web applications are represented
- 0:07
as a complex tree of components
- 0:09
components that constantly produce and
- 0:11
share data called state and when state
- 0:12
is decentralized it can become difficult
- 0:14
to understand and test redux is both a
- 0:17
pattern and library that helps
- 0:18
developers implement complex state
- 0:20
management requirements at scale it was
- 0:22
created by dan abramov and andrew clark
- 0:25
at facebook and is one of the most loved
- 0:27
and hated libraries in the react
- 0:28
ecosystem it relies on a single
- 0:30
immutable object to store all of the
- 0:32
application state kind of like a
- 0:34
client-side database to change the state
- 0:36
like when a button is clicked an action
- 0:38
will be dispatched which has a name like
- 0:40
an event and a payload with the data
- 0:42
that it wants to change remember the
- 0:43
store is immutable so to change the
- 0:45
state of the application an entirely new
- 0:47
object is created by passing the current
- 0:49
state and the action payload into a
- 0:51
reducer function which returns a new
- 0:53
object with the entire application state
- 0:56
the end result is a one-way data flow
- 0:58
that is predictable and testable and it
- 1:00
also opens the door to awesome dev tools
- 1:02
that allow you to time travel through
- 1:03
your application's data but it comes at
- 1:05
a cost of additional boilerplate code
- 1:07
that might add too much complexity to a
- 1:09
smaller project to get started create a
- 1:11
react app with your favorite tool then
- 1:13
install the redux toolkit first use
- 1:15
configure store to set up the global
- 1:17
store object which will register any
- 1:19
reducers defined elsewhere in the code
- 1:21
then provider will make its data
- 1:23
accessible to the entire component tree
- 1:25
next create a slice like pizza to
- 1:27
represent some data in the store it
- 1:29
should have a unique name and initial
- 1:30
state but most importantly it contains a
- 1:33
collection of reducers which are
- 1:34
functions that take the old state and an
- 1:37
action then define the logic required to
- 1:39
change the state redux toolkit will
- 1:41
automatically generate actions that
- 1:43
correspond to the names of these reducer
- 1:45
functions we can export them then put
- 1:47
them to use in an actual ui component
- 1:49
the beauty of redux is that we can
- 1:51
select data anywhere in the application
- 1:53
without the need for context or prop
- 1:55
drilling instead we can grab any
- 1:57
reactive value or slice in the store
- 1:59
with the use selector hook now to change


