'How do I get state from one slice to another? Redux toolkit
I'm making a small MERN application and I'm interested in how to transfer a user from authSlice, which has a user object where I place the user when fetching, to profileSlice? Because in profileSlice I want to have logic about follow / unfollow. But I need a user to manipulate it.
const initialState = {
user: null,
isError: false,
isSuccess: false,
isLoading: false,
isSuccessVerify: false,
message: '',
isAuthenticated: false,
loadingUser: true,
};
export const authSlice = createSlice({
name: 'auth',
initialState,
reducers: {
reset: state => {
state.isLoading = false;
state.isError = false;
state.isSuccess = false;
state.message = '';
},
},
extraReducers: {
......................
// Login
[login.pending]: state => {
state.isLoading = true;
state.isAuthenticated = false;
},
[login.fulfilled]: (state, action) => {
state.isLoading = false;
state.isSuccess = true;
state.isAuthenticated = true;
state.user = action.payload.user;
},
[login.rejected]: (state, action) => {
state.isLoading = false;
state.isError = true;
state.message = action.payload;
},
.................
});
I want to use that user I put in the authSlice initial state to profileSlice. How i can do this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
