I need to pass userId
and userNickname
to action authUser
in authSlice
reducer.
authSlice:
JavaScript
x
25
25
1
import { createSlice } from '@reduxjs/toolkit';
2
3
const initialState = {
4
userId : null,
5
userNickame : "",
6
isAuth : false,
7
isAdmin : false,
8
}
9
10
export const authSlice = createSlice({
11
name : 'auth',
12
initialState,
13
reducers : {
14
authUser : (state, action) => {
15
//const {userId, userNickame} = action.payload;
16
state.userId = userId;
17
state.userNickame = userNickame;
18
state.isAuth = true;
19
},
20
},
21
})
22
23
export const { authUser } = authSlice.actions
24
export default authSlice.reducer
25
calling dispatcher
JavaScript
1
10
10
1
//...
2
3
const dispatch = useDispatch();
4
const userId = useSelector((state) => state.userId);
5
const userNickname = useSelector((state) => state.userNickname);
6
7
//...
8
9
dispatch(authUser(/* I need to pass userId and userNickname here */));
10
Advertisement
Answer
Simply
dispatch(authUser({ userId, userNickname }));
which is equivalent to
dispatch(authUser({ userId: userId, userNickname: userNickname }));