学习代码的时候,一定要动手实践,否则根本学不会!
import { createStore, applyMiddleware } from "redux";
import { combineReducers } from "redux"
//引入中间键盘
import logger from "redux-logger"
import thunk from "redux-thunk"
// 两个reducer 我开始测试 combineReducer
function nameReducer(state = { "name": "action" }, action) {
const type = action.type;
switch (type) {
case "get":
return state;
default:
return state;
}
}
function CountReducer(state = 10, action) {
const type = action.type;
switch (type) {
case "add":
state++;
return state;
case "minus":
state--;
return state;
case "asyncAdd":
// 我打印这个就是想看下,它咋接收参数的!
console.log(action);
state++;
return state;
default:
return state;
}
}
// applyMiddleware 使用中间键
const store = createStore(combineReducers({
count: CountReducer,
name: nameReducer
}), applyMiddleware(logger, thunk));
export default store;
上面就是combineReducers 的使用方式,我要重点说的是下面的使用方式
显示效果
总结下,说白了,就是映射属性时,必须带key ,key 用来区分不同的reducer
好,属性我们会了,我们再测试下方法
方法和原来一样
行,到此我们redux 就基本学会了,会用redux react-redux react-thunk 包括combineReducers 的使用
好,就到这里,祝大家学习愉快,生活幸福