一、创建目录

二、拆分 reducer
1)创建 reducer
const initState = 0;
const countReducer = (state = initState, { type, payload }) => {
switch (type) {
default: return state;
}
}
export default countReducer;
2)合并 reducer
import { combineReducers } from 'redux';
import countReducer from './counter/reducers.js'
export default combineReducers({
count: countReducer
});
3)仓库引入 reducer
import { createStore } from "redux";
import combineReducers from './combineReducers.js'
const store = createStore(combineReducers);
三、拆分 action
1)创建 action 对象
export const incrementAction = () => {
return {
type: 'increment'
}
}
export const decrementAction = () => {
return {
type: 'decrement'
}
}
export const inputCountAction = payload => {
return {
type: 'inputCount',
payload
}
}
2)提取 type 值
const INCREMENT = 'increment';
const DECREMENT = 'decrement';
const INPUT_COUNT = "inputCount";
export default {
INCREMENT, DECREMENT, INPUT_COUNT
}
3)修改 action 对象
import types from './actionTypes.js';
export const incrementAction = () => {
return {
type: types.INCREMENT
}
}
export const decrementAction = () => {
return {
type: types.DECREMENT
}
}
export const inputCountAction = payload => {
return {
type: types.INPUT_COUNT,
payload
}
}
4)修改 reducer 文件
import types from './actionTypes.js'
const initState = 0;
const countReducer = (state = initState, action) => {
switch (action.type) {
case types.INCREMENT: return state + 1;
case types.DECREMENT: return state - 1;
case types.INPUT_COUNT: return action.payload;
default: return state;
}
}
export default countReducer;