更新一个切片的状态,来自另一个切片。

huangapple go评论51阅读模式
英文:

Update state of an slice from another slice

问题

I am using redux-toolkit和rtk query。我有一个场景,在用户登录到应用程序时更新我的auth状态,并且当用户更新他的信息时,我想要更新用户信息(authSlice),但是我将它们存储在两个不同的slices authuser中。有没有办法在用户在userSlice中更新信息时更新auth状态?

在这里的最佳实践是什么?如果没有办法在不同的slice中更新另一个slice的状态,我应该将这两个状态合并在一起吗?

userSlice

export const userSlice = createSlice({
	name: "user",
	initialState: {
		dietPlan: null,
		workout: null,
		dailyDietPlan: null,
		dailyWorkout: null,
		information: null,
	} as InitialState,
	reducers: {},
	extraReducers: builder => {
		builder.addMatcher(
			userApi.endpoints.updateUserInfo.matchFulfilled,
			(state, {payload}) => {
				console.log(payload);
				this.dispatch(updateUser(payload.data)); // 有办法做到这一点吗?
			}
		);
	}
});

export default userSlice.reducer;

authSlice

const authSLice = createSlice({
	name: "auth",
	initialState: { user: null, token: null } as AuthState,
	reducers: {
		updateUser(state, action) {
			state.user = action.payload;
		}
	},
	extraReducers: builder => {
		builder.addMatcher(
			authApi.endpoints.login.matchFulfilled,
			(state, {payload}) => {
				state.user = payload.data;
			}
		);
		builder.addMatcher(
			authApi.endpoints.logout.matchFulfilled,
			(state) => {
				state.user = null;
			}
		);
	}
});

export default authSLice.reducer;

export const { updateUser } = authSLice.actions;
英文:

I am using redux-toolkit and rtk query. I have scenario which I update my auth state when user login to the application and I want to update the user information(authSlice) when user updates his information but I am storing them in to 2 different slices auth and user. Is there any way to update auth state when user updates his information which takes place in userSLice ?

what is the best practice here? Should I combine 2 sates together if there is no way to update the state of another slice inside a different slice?

userSlice

export const userSlice = createSlice({
	name: "user",
	initialState: {
		dietPlan: null,
		workout: null,
		dailyDietPlan: null,
		dailyWorkout: null,
		information: null,
	} as InitialState,
	reducers: {},
	extraReducers: builder => {
		builder.addMatcher(
			userApi.endpoints.updateUserInfo.matchFulfilled,
			(state, {payload}) => {
				console.log(payload)
				this.dispatch(updateUser(payload.data)) // is there any way to do this ?
			}
		)
  }
})

export default userSlice.reducer

authSlice

const authSLice = createSlice({
	name: "auth",
	initialState: { user: null, token: null } as AuthState,
	reducers: {
		updateUser(state, action) {
			state.user = action.payload
		}
	},
	extraReducers: builder => {
		builder.addMatcher(
			authApi.endpoints.login.matchFulfilled,
			(state, {payload}) => {
				state.user = payload.data
			}
		)
		builder.addMatcher(
			authApi.endpoints.logout.matchFulfilled,
			(state) => {
				state.user = null
			}
		)
	}
})

export default authSLice.reducer

export const { updateUser } = authSLice.actions

答案1

得分: 1

以下是您要翻译的内容:

"从另一个切片访问状态的方式是不存在的,这是有意设计的。因此,如果任何人想要在 rtk 查询完成/拒绝后更新另一个切片的状态,他们必须创建一个更新 Redux 中数据的操作,并检查 status === "fulfilled" 然后分发该特定操作。

这是将在本地更新数据的 reducer

type AuthState = {
	user: User | null
}

const authSlice = createSlice({
	name: "auth",
	initialState: { user: null } as AuthState,
	reducers: {
		updateUserLocally(state, action) {
			state.user.info = action.payload.info
		}
	},
	extraReducers: builder => {
		builder.addMatcher(
			authApi.endpoints.login.matchFulfilled,
			(state, { payload }) => {
				state.user = payload.data
			}
		)
		builder.addMatcher(
			authApi.endpoints.logout.matchFulfilled,
			(state) => {
				state.user = null
			}
		)
	}
})

export default authSlice.reducer

export const { updateUserLocally } = authSlice.actions

以及在我的组件内部


	const user = useAppSelector(store => store.auth.user)
	const dispatch = useAppDispatch()
	
	const [updateUser, { status, data, error }] = useUpdateUserInfoMutation()
	
	const onSubmit = (data: any) => {
		updateUser(data)
	}
	
	if (status === 'fulfilled') {
		dispatch(updateUserLocally(data.data)) // 这是操作
	}

"

英文:

There is no way to access the sate from another slice and this is intentional by design. So if any one wants to update another slice state after an rtk query fullfilled/rejected then they have to create an action which updates the data locally in redux and check to see if the status === "fulfilled" then dispatch that particular action.

this is the reducer which will update the data locally

type AuthState = {
	user: User | null
}

const authSLice = createSlice({
	name: "auth",
	initialState: { user: null } as AuthState,
	reducers: {
		updateUserLocally(state, action) {
			state.user.info = action.payload.info
		}
	},
	extraReducers: builder => {
		builder.addMatcher(
			authApi.endpoints.login.matchFulfilled,
			(state, {payload}) => {
				state.user = payload.data
			}
		)
		builder.addMatcher(
			authApi.endpoints.logout.matchFulfilled,
			(state) => {
				state.user = null
			}
		)
	}
})

export default authSLice.reducer

export const { updateUserLocally } = authSLice.actions

and inside my component


	const user = useAppSelector(store => store.auth.user)
	const dispatch = useAppDispatch()
	
	const [updateUser, {status, data, error}] = useUpdateUserInfoMutation()
	
	const onSubmit = (data: any) => {
		updateUser(data)
	}
	
	if (status === 'fulfilled') {
		dispatch(updateUserLocally(data.data)) // this is the action
	}

huangapple
  • 本文由 发表于 2023年5月14日 13:33:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定