在slices redux工具包之间共享状态?

发布于 2025-02-11 00:27:33 字数 1738 浏览 0 评论 0原文

任何想法如何将状态数据从一个切片传递到另一个切片,换句话说,切片B可以访问slice A ...它尝试在另一个切片中使用getState(),但是它会导致参考错误,什么是正确解决这个问题吗?

// Slice A 
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { RootState } from './store';

type userType = { user: ''} 

export interface globalState {
  currentUser: userType;
}

const initialState: globalState = {
  currentUser: { user: '' },
};

export const globalSlice = createSlice({
  name: 'global',
  initialState,
  reducers: {
    setUser: (state, action: PayloadAction<{ user: string }>) => {
      state.currentUser = { user: action.payload.user }
    },
  },
});

export const GlobalCurrentState = (state: RootState) => state.global;
export const { setUser} = globalSlice.actions;

export default globalSlice.reducer;
// Slice B
import { RootState, store } from './store';
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface infoState {
  info: { data: string };

}

const initialState: infoState = {
  info: { data : '' },
};

export const infoSlice = createSlice({
  name: 'info',
  initialState,
  reducers: {
    addName: (state, action: PayloadAction<{ select: string; value: string }>) => {
      const updateData = async () => {
      // this line is casuing a refrence erorr the slice cannot see the info form the other slice. 
      let fromStateA = store.getState().currentUser
    },
});

// Values
export const infoCurrentState = (state: RootState) => state.info;
// Action creators are generated for each case reducer function
export const { addName } = infoSlice.actions;
export default infoSlice.reducer;

any idea how to pass state data from one slice to another slice , in other words slice b can have access to slice a... it tried to use getState() inside the other slice but it is causing a reference error , what is the right way to go about this ?

// Slice A 
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { RootState } from './store';

type userType = { user: ''} 

export interface globalState {
  currentUser: userType;
}

const initialState: globalState = {
  currentUser: { user: '' },
};

export const globalSlice = createSlice({
  name: 'global',
  initialState,
  reducers: {
    setUser: (state, action: PayloadAction<{ user: string }>) => {
      state.currentUser = { user: action.payload.user }
    },
  },
});

export const GlobalCurrentState = (state: RootState) => state.global;
export const { setUser} = globalSlice.actions;

export default globalSlice.reducer;
// Slice B
import { RootState, store } from './store';
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface infoState {
  info: { data: string };

}

const initialState: infoState = {
  info: { data : '' },
};

export const infoSlice = createSlice({
  name: 'info',
  initialState,
  reducers: {
    addName: (state, action: PayloadAction<{ select: string; value: string }>) => {
      const updateData = async () => {
      // this line is casuing a refrence erorr the slice cannot see the info form the other slice. 
      let fromStateA = store.getState().currentUser
    },
});

// Values
export const infoCurrentState = (state: RootState) => state.info;
// Action creators are generated for each case reducer function
export const { addName } = infoSlice.actions;
export default infoSlice.reducer;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

魂归处 2025-02-18 00:27:33

这是不打算的,也不可能 - Redux中的切片彼此独立,除了自己的状态和所传递的动作内容外,世界上一无所知。您可以尝试某种自定义的手写还原器,或像降低 - 还原器之类的库,但是大多数情况下,您要么将逻辑分为一片,要么将其分为两部分,或者您不会将足够的信息传递到操作中。

This is not intended and not possible - slices in Redux are independent of each other and know about nothing in the world except about their own state and the contents of the action being passed in. You can experiment with some custom kind of hand-written reducer, or a library like reducer-reducers, but most of the time you are either splitting logic that should be one slice into two, or you don't pass enough information into your action.

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