如何将对象传递给切片中的动作



我需要将userIduserNickname传递给authSlice减速器中的动作authUser

authSlice:

import { createSlice } from '@reduxjs/toolkit';
const initialState = {
userId : null,
userNickame : "",
isAuth : false,
isAdmin : false, 
}
export const authSlice = createSlice({
name : 'auth',
initialState,
reducers : {
authUser : (state, action) => {
//const {userId, userNickame} = action.payload;
state.userId = userId;
state.userNickame = userNickame;
state.isAuth = true;
},
},
})
export const { authUser } = authSlice.actions
export default authSlice.reducer

呼叫调度员

//...
const dispatch = useDispatch();
const userId = useSelector((state) => state.userId);
const userNickname = useSelector((state) => state.userNickname);
//...
dispatch(authUser(/* I need to pass userId and userNickname here */));

简单

dispatch(authUser({ userId, userNickname }));

相当于

dispatch(authUser({ userId: userId, userNickname: userNickname }));

相关内容

  • 没有找到相关文章

最新更新