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 StoreStore:
Reducers, combineReducers, createStore (if using middleware, import applyMiddleware, thunk)combineReducers() all ReducerscreateStore with param rootReducer (if using middleware, add applyMiddleware(thunk) in param);StoreReducer:
ActionsinitialStateexport Reducer function as a series of switch/case ActionsinitialState with whatever data Actions 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 actionFunctionComponent:
actionFunction, connectmapStateToProps() the rootReducer state from StoreStore.rootReducer.Reducerconnect mapStateToProps and actionFunction with ComponentActionActionReducer
// ./src/actions.js
export const GET_TRANSACTIONS = 'GET_TRANSACTIONS';
export const getTransactions = {
type: GET_TRANSACTIONS,
payload: []
};
ReducerAction definitionstateReducer 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;
ProviderProvider from react-redux packageStoreComponentStore 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);