2. Redux
What’s Redux?
Is a predictable state management container library for JavaScript
apps. Itself is a small library of 2KB. It was release in June 2, 2015
by Dan Abramov.
What is a state?
Is a data that React component holds to render to the
user.
Before understand what is Redux need
to understand what’s a state…
4. What does the Redux solve?
Even a silly to-do list could become
unmanageable if you starting passing the state
up and down.
Examples of state
• Data the user sees
• Data we fetch
• URL we are showing to the user
• Selected items inside the page
• Errors messages in the applications
Unmanageable states…
5. Things will become tricky…
5
“Without
Redux your
React app
passes state
up and down
the tree
nodes…”
“With Redux
your React app
dispatches
updates to
states to a
single tree
node…”
6. When to use Redux?
• Multiple React components needs to access the same state but do not
have any parent/child relationship
• You start to feel awkward passing down the state to multiple
components with props
7. How all parts go together?
7
Store
Orchestrates all the moving pats in Redux. The state of the whole lives inside the store.
Reducer
A JavaScript function that takes two arguments, current state and an action. The state must
return entirely from reducers. Then data state are never mutated, that only copied.
Action
Sends out signal to the store to change the state called dispatching an action.
Container
When you connect your component to Redux, you connect with two arguments based use cases.
• mapStateToProps – Connect parts of the Redux state to the props of a React Component.
So, your component can have exact part of the store it needs.
• mapDispatchToProps – Connects Redux actions to React props. This way your component
will be able to dispatch actions.
8. Store
8
// src/js/store/index.js
import { createStore } from "redux";
import rootReducer from "../reducers/index";
const store = createStore(rootReducer);
export default store;
Store methods
• getState for accessing the current state of the application
• dispatch for dispatching an action
• subscribe for listening on state changes
Code: