Redux work flow
Install npm install --save redux
Redux is like a central store which stores entire application state.
Reducer:
Is a function, accepts two arguments: (prevState, action)
.
1 | // redux-basics.js |
node redux-basics.js
In project
- Install
npm install --save react-redux
Provider
: allows us to inject store into react component.
1 | // Index.js |
- Create a
store
folder undersrc
directory - Create a file named
reducer.js
instore
1 | const initialState = { |
Subscribe to the dispatch
connect
is a function that returns a function as higher order component. In the argument, it accepts:
- Which `state` do we want to change
- Which `action` do we want to dispatch
1 | // In container component |
Dispatch Actions to update global state
1 | // In container component |
In reducer.js
1 | const reducer = (state = initialState, action) => { |
Oursourcing Actions
1 | // actions.js |
1 | // reducer.js |
1 | // component.js |