常规用法: dispatch后会更新数据
import { useDispatch, useSelector, useStore } from "react-redux"
const dispatch = useDispatch()
// 通过 useSelector 获取整个redux的对象,再获取里面的数据
const DirectoryTree = useSelector(state => state.reduxStore1.DirectoryTree)
dispatch({ type: 'inser' , data: 10 })
进一步解释 dispatch 之后的数据流向哪 :
const initialState = { count:2 , kao:8 }
export const reduxStore2 = (state = initialState, action) => {
switch (action.type) {
case 'inser':
return Object.assign({}, state, { kao : action.data })
}
然后根据reducer 创建一个store :
const rootReducer = combineReducers({
reduxStore1: reduxStore1,
store2: reduxStore2,
});
const store = createStore(rootReducer)
再然后在 app.js 文件注入
function App() {
<Provider store={store}>
<AppRoute />
</Provider>
}
----------------------------------------------------
1、 Redux 中 dispatch时,只会进行数据浅比较 。
只要第一层,地址发生改变,就会数据刷新。
如果,第一层是基本数据类型,那么直接赋值即可 。
2、如果加了redux持久化这个包,每次修改 initialState里面都初始变量,都需要重新启动项目,并清空缓存。( state = initialState,)