示例如下
import { createStore,combineReducers } from "redux";
//拆分reducer
//城市reducer
const cityReducer = (pre = {
cityName:'北京'
},action)=>{
const newState = {...pre}
switch(action.type){
case 'cxk':
newState.cityName = action.value
return newState
default:
return pre
}
}
//tab reducer
const tabReducer = (pre = {
show:false
},action)=>{
const newState = {...pre}
switch(action.type){
case 'yes':
newState.name = true
return newState
case 'yes':
newState.name = false
return newState
default:
return pre
}
}
const reducer = combineReducers({ //合并两个reducer
cityReducer,
tabReducer
})
const store = createStore(reducer); //创建一个store
export default store
使用方式如下
const btn = ()=>{
store.dispatch({ //点击派发
type:'cxk',
value:'赣州'
})
setName('sss')
}
<h1>{store.getState().cityReducer.cityName}</h1>