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

How to pass the object to action in slice?

我需要将 userIduserNickname 传递给 authSlice reducer 中的操作 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 }));