我需要将userId
和userNickname
传递给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 }));