多個reducer用combinReducers包裹杆麸,
里面以對象的格式包裹
import {createStore,combineReducers} from 'redux'
import {brands,brand} from './reducer'
let reducer = combineReducers({
a:brands,
b:brand
})
let store = createStore(reducer)
export default store
不同的reducer使用里面的type判斷也不要相同,不然dispatch的時候都會走兩個reducer的那個type類型
const initData = []
function brands(state = initData, action) {
switch (action.type) {
case 'add':
const newArr = [...state]
newArr.unshift(action.data)
return newArr
case 'delete':
return state.filter(v => v.id !== action.data)
case 'updata':
return [action.data]
default:
return state
}
}
const initDatas = []
function brand(state = initDatas, action) {
switch (action.type) {
case 'aaa':
const newArr = [...state]
newArr.unshift(action.data)
return newArr
case 'bbb':
return state.filter(v => v.id !== action.data)
case 'ccc':
return [action.data]
default:
return state
}
}
export {
brand,brands
}
store.dispatch({ type: 'updata', data: {id:'1'} })
檢測store改變時圆到,會走回調(diào)函數(shù)
store.subscribe(()=>{
console.log(store.getState());
})
這里兩個reducer 一個是a一個是b剧浸,這里拿a的值來渲染
let [dataList, setDataList] = useState(store.getState().b)
const columns = [
{
id: '1',
dataIndex: 'id',
}
]
return (
<>
<Table dataSource={dataList} columns={columns} rowKey='id' />
</>
)