使用store.dispatch与redux工具包切片

发布于 2025-02-13 10:41:40 字数 855 浏览 1 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蘑菇王子 2025-02-20 10:41:40

我也有同样的问题,刚刚起作用。在普通的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))

暮色兮凉城 2025-02-20 10:41:40

这是underispatch react-redux库中的函数:

function useDispatch() {
    const store = useStore()
    return store.dispatch
  }

您可以从store.js中导出商店,并使用store.dispatch < /代码>

This is what useDispatch function in react-redux library:

function useDispatch() {
    const store = useStore()
    return store.dispatch
  }

You can export the store from store.js and use store.dispatch

彼岸花似海 2025-02-20 10:41:40

我总是从社区中获得的回击,而不是直接从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 the Provider 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文