使用store.dispatch与redux工具包切片
我有一个redux工具包切片,如下所示,
import { createSlice } from '@reduxjs/toolkit';
const initialState = {
value: null,
};
export const authSlice = createSlice({
name: 'auth',
initialState,
reducers: {
setAuth: (state, action) => {
state.value = action.payload;
},
},
});
export const { setAuth } = authSlice.actions;
export default authSlice.reducer;
我可以在函数组件中使用underispatch
,如下所示,
const dispatch = useDispatch();
const auth = useSelector((state) => state.auth.value);
const setAuthValue = (value) => {
dispatch(setAuth(value));
};
但是,我有一个场景,我无法为此code> underispatch
挂钩。 (例如:Inside Axios Interceptor)
是否有可能直接使用store.dispatch({'type':'',有效载荷:{}}
像普通Redux一样,带有上述REDUX TOOLSKIT SLISE?
或什么是更好的方法?
I have a redux toolkit slice as follow
import { createSlice } from '@reduxjs/toolkit';
const initialState = {
value: null,
};
export const authSlice = createSlice({
name: 'auth',
initialState,
reducers: {
setAuth: (state, action) => {
state.value = action.payload;
},
},
});
export const { setAuth } = authSlice.actions;
export default authSlice.reducer;
I can use useDispatch
in function component as follow to setAuth
const dispatch = useDispatch();
const auth = useSelector((state) => state.auth.value);
const setAuthValue = (value) => {
dispatch(setAuth(value));
};
However, I have a scenario where I am not able to useDispatch
hooks for this. (eg: inside axios interceptor)
Is that possible that I directly use store.dispatch({'type': '', payload: {}}
like normal redux with above redux toolkit slice?
Or what is the better approach for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也有同样的问题,刚刚起作用。在普通的JS文件(在组件函数上下文之外)中,只需导入您从切片中导出的操作即可。然后,您可以像这样派遣:
store.dispatch(setauth(pareload))
I had the same question and just got it working. In the plain js file (outside a component function context), just import your
setAuth
action that you're exporting from your slice. Then, you can dispatch it like so:store.dispatch(setAuth(payload))
这是
underispatch
react-redux
库中的函数:您可以从
store.js
中导出商店,并使用store.dispatch < /代码>
This is what
useDispatch
function inreact-redux
library:You can export the store from
store.js
and usestore.dispatch
我总是从社区中获得的回击,而不是直接从
store
直接派遣提供商
而不是直接访问。但是,在我的后袋中,我已经实施了带有多家商店的项目,并得到了Redux维护者现在在其文档中每个项目只建议一个商店的反馈: https://redux.js.org/tutorials/fundamentals/part-4-store#redux-store
也许非Singelton建议可能更重要,例如测试VS生产类型的情况。但是鉴于维护者每个项目只建议1个商店,我认为没有任何理由不直接从商店派遣。
The pushback that I always got from the community around not dispatching directly from the
store
is that it was never meant to be a singleton (there could be more than one store) and that the store should be derived from theProvider
rather than accessed directly.However, having that in my back pocket, I have implemented projects with multiple stores and got the feedback that the Redux maintainers are suggesting only a single store per project in their documentation now: https://redux.js.org/tutorials/fundamentals/part-4-store#redux-store
Perhaps the non-singelton suggestion is meant more for like a test vs production type situation. But given that the maintainers are suggesting only 1 store per project, I don't see any reason not to dispatch directly from the store.