在slices redux工具包之间共享状态?
任何想法如何将状态数据从一个切片传递到另一个切片,换句话说,切片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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不打算的,也不可能 - 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.