Fireship · @fireship
Redux from Scratch
Uploaded June 27, 2018 · Length 9:46
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.
- 0:00
[Music]
- 0:06
why use a state management library and
- 0:08
angular when you can just build your own
- 0:10
from scratch when you combine rxjs with
- 0:12
angular's dependency injection it
- 0:14
provides a powerful foundation for
- 0:16
building your own state management
- 0:17
system that means you can get all the
- 0:19
benefits of the redux pattern including
- 0:20
the dev tools that you see here on the
- 0:22
right but you can create your own
- 0:23
abstractions and conventions to reduce
- 0:25
the amount of boilerplate code plus
- 0:27
you'll have a much better understanding
- 0:28
of how your app works under the hood
- 0:30
over the next few minutes you'll learn
- 0:31
how to use custom rxjs operators and
- 0:33
multi casting to build your own state
- 0:36
management library but mostly I just
- 0:37
want to show you how this pattern works
- 0:39
at a fundamental level if you're new
- 0:41
like and subscribe and grab the source
- 0:42
code from angular firebase comm in case
- 0:45
you're not familiar with the redux
- 0:46
pattern here's a quick refresher the
- 0:48
whole idea is that we treat our app data
- 0:50
or state as a global immutable object
- 0:53
the only way we can change the current
- 0:54
state of the app is by dispatching an
- 0:57
action so that might happen on a button
- 0:59
click then the payload for that action
- 1:00
will go to a reducer function to
- 1:02
determine what the next state should
- 1:04
look like the store itself isn't
- 1:06
observable so everything will happen in
- 1:08
a reactive way so anything that
- 1:10
subscribes to the store will be updated
- 1:12
with the latest data the benefit of this
- 1:14
is that we now have a one-way data flow
- 1:16
that provides a predictable history of
- 1:18
all the changes that occur in the app so
- 1:19
we can do time-travel debugging and cool
- 1:21
stuff like that if we think about this
- 1:24
in code our current state is equal to
- 1:26
the stream of actions and the function
- 1:28
that reduces them a reducer is just a
- 1:30
pure function that takes in the current
- 1:32
state and the action and then returns
- 1:34
the next state so let's go ahead and
- 1:36
build this thing the first thing I want
- 1:37
to do is generate a service called store
- 1:39
and I'd also like to point out that I
- 1:41
have lodash installed not only will it
- 1:43
dramatically simplify our code but it
- 1:45
will also give us better performance
- 1:47
than we'd be able to achieve natively so
- 1:49
first I want to show you how our store
- 1:51
service will be used in a component one
- 1:53
really important concept in redux is
- 1:54
that our components should be stateless
- 1:56
meaning they should only be listening to
- 1:58
data from the store as an observable or


