React Redux
React Redux
1. What is Redux?
2. Why Redux?
3. Vocabulary
4. Principles
5. The Redux lifecycle
6. Workshop: catbook integration
7. Good practices
8. Resources
9. Homework
What is Redux?
Redux is a predictable state container for
JavaScript apps.
Why Redux?
GET /api/whoami
App
this.props.userId
Navbar Feed this.props.userId
SingleComment NewComment
Redux
erId Store
ps.us
.pro
s
thi rI
d
App e
.us
s
rop
p
is.
Navbar Feed th
this.props.userId
NewStory Card
SingleComment NewComment
Redux Vocabulary
• Store: the global app state
• Actions: objects that describe “what happened”
• Reducers: functions that return the next state based on an action
• Dispatching: triggering a state update by sending an action
Redux Principles
1) Your app state (Redux store) is the
single source of truth.
2) Like in React, the state is read-only.
Never mutate your state directly.
3) State changes are made with pure functions
(no side effects).
E.g. Don’t make an API call inside of a reducer.
= Redux concept = React concept
Reducer Store
Create a new state
{
type: string, REQUIRED
...,
}
additional data
Reducers (pure functions)
return the next state
function(state, action) {
switch (action.type) {
case UPDATE_USER_ID:
return Object.assign({}, state, {
userId: action.userId,
});
...
}
}
{
user: {
userId: string,
user: object,
}, Catbook
story: {
Redux Store
stories: [object],
},
}
Workshop: Integrating Redux into Catbook
If you plan on following along:
• Rule of thumb: put data in the Redux store when it’s shared between
two or more components.
• E.g. You probably don’t need to put the state of an input textbox in the redux store.
story: {
stories: [object],
},
}
Thanks!
Keith Orlando
[email protected]