0% found this document useful (0 votes)
75 views21 pages

React Redux

React-redux

Uploaded by

Govind Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views21 pages

React Redux

React-redux

Uploaded by

Govind Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

React + Redux

Managing Your React App State with Redux


Keith Orlando
Agenda

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
[Link]
Navbar Feed [Link]

NewStory Card [Link]

“Lift up state” to [Link]


SingleStory CommentsBlock [Link]
and pass it down as props

SingleComment NewComment
Redux
erId Store
[Link]
.pro
s
thi rI
d
App e
.us
s
rop
p
is.
Navbar Feed th

[Link]
NewStory Card

“Lift up state” to the


SingleStory CommentsBlock
Redux Store

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

Dispatch the action


Subscribe to the store
(you choose what data is
Redux
available as props)
Lifecycle

Action Creator Component (View)


Create an action
Actions (objects)
describe what happened

{
type: string, REQUIRED

...,
}
additional data
Reducers (pure functions)
return the next state

current part of state dispatched action (an object)

function(state, action) {
switch ([Link]) {
case UPDATE_USER_ID:
return [Link]({}, state, {
userId: [Link],
});
...
}
}
{
user: {
userId: string,
user: object,
}, Catbook
story: {
Redux Store
stories: [object],
},
}
Workshop: Integrating Redux into Catbook
If you plan on following along:

1. Control + C (quit) any currently running instances of catbook/react hot loader


2. Open a terminal window/tab and type:

$ cd ~ (or wherever you want to clone the project)


$ git clone [Link]
$ cd catbook-redux
$ npm install
$ npm start
$ (open the project in another terminal) npm run hotloader

3. Visit [Link] in your browser


Redux Good Practices
(when not to use Redux?)

• 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.

• Redux and React are not highly opinionated frameworks. Pick a


convention and stick to it for consistency.
• Applies to naming conventions
• Applies to making API calls (I prefer making an API call in componentDidMount and then
dispatching an action)
Additional Resources
• Redux Docs
[Link]
• The redux-workshop branch of my fork
$ git reset –hard
$ git fetch
$ git checkout redux-workshop
• Redux Sagas for advanced asynchronous handling
[Link]
• Redux Sauce for quicker setup
[Link]
Homework
Upgrade the rest of catbook to redux.
{
user: {
userId: string,
user: object,
},

story: {
stories: [object],
},
}
Thanks!
Keith Orlando
korlando@[Link]

You might also like