Based on this tutorial.
Index
:
Provider
<Provider>
as a param inside ReactDOM.render
, before the other param document.getElementById('root')
<Provider>
attribute store
value to Store
Store
:
Reducer
s, combineReducers
, createStore
(if using middleware, import applyMiddleware
, thunk
)combineReducers()
all Reducer
screateStore
with param rootReducer
(if using middleware, add applyMiddleware(thunk)
in param);Store
Reducer
:
Action
sinitialState
export
Reducer
function as a series of switch
/case
Action
sinitialState
with whatever data Action
s need to useAction
:
ACTION
in upper case, with type
and some stuffingexport
function actionFunction
under each ACTION
with same name in camel caseactionFunction
, return an anonymous function with dispatch
as paramthen
promise of the async call, dispatch
that actionFunction
Component
:
actionFunction
, connect
mapStateToProps()
the rootReducer
state from Store
Store.rootReducer.Reducer
connect
mapStateToProps
and actionFunction
with Component
Action
Action
Reducer
// ./src/actions.js
export const GET_TRANSACTIONS = 'GET_TRANSACTIONS';
export const getTransactions = {
type: GET_TRANSACTIONS,
payload: []
};
Reducer
Action
definitionstate
Reducer
functionstate
in Reducer
functionstate
and any other dataReducer
for use in Store
```javascript
// ./src.reducers.js
import { GET_TRANSACTIONS } from ‘./src/actions’;const initialState = {}; export const transactionReducer = (state=initialState, action) => { switch(action.type) { case GET_TRANSACTION: axios.get(‘/transactions’) return state, action.payload default: return state } };
### `Store`
1. Import **`Reducer`** s
2. Combine **`Reducer`** s
3. Create **`Store`** using the combined **`Reducer`** s
4. Export **`Store`** for use in **`<Provider>`**
```javascript
import { combineReducers, createStore } from 'redux';
import { transactionReducer } from './src/reducers';
const rootReducer = combineReducers({
transaction: transactionReducer
// ... other reducers
});
const store = createStore(rootReducer);
export default store;
Provider
Provider
from react-redux
packageStore
Component
Store
in Provider
; this makes Component
aware of the Store
```javascript
// ./src/index.js
import React from ‘react’;
import ReactDOM from ‘react-dom’;
import { Provider } from ‘react-redux’;
import store from ‘./src/store’;
import App from ‘./components/app/App’;
import { BrowserRouter } from ‘react-router-dom’;ReactDOM.render(
<Provider store={store}>
### `Component`
1. Import **`Action`** into **`Component`**
2. Map `state` values and actions to **`Props`**
3. Export the connection
```javascript
import React from 'react';
import { connect } from 'react-redux';
import { getTransactions } from './src/actions';
class TransactionContainer extends React.Component {
...
}
// `state.transaction` is from `rootReducer` in store.js
// this component has access to store because it is encapsulated by Provider
const mapStateToProps = (state) => {
return {
transaction: state.transaction
// other values or actions from state
}
}
export default connect(mapStateToProps, getTransactions)(TransactionContainer);