redux

多个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改变时,会走回调函数

store.subscribe(()=>{
   console.log(store.getState());
  })

这里两个reducer 一个是a一个是b,这里拿a的值来渲染

  let [dataList, setDataList] = useState(store.getState().b)
    const columns = [
        {
            id: '1',
            dataIndex: 'id',
        }
    ]
    return (
        <>
            
        
    )

你可能感兴趣的:(redux)