'Redux selector erroring when trying to export state in one slice but not another when the code is almost similar
I am new to redux and I have created a slice called eventmodalSlice that is supposed to keep track of whether a modal in the app is open or closed. I get an error when I create a selector to export the state. The error says
TypeError: undefined is not an object (evaluating 'state.eventmodal')
import React from "react"
import { createSlice } from "@reduxjs/toolkit"
export const eventmodalSlice = createSlice({
name: "eventmodal",
initialState:{
modalvisible: null
},
reducers: {
Close: (state, action) => {
state.modalvisible = action.payload;
},
Open: (state, action) => {
state.modalvisible = action.payload;
},
}
});
export const {Close, Open} = eventmodalSlice.actions;
export const selectEventModal = (state) => state.eventmodal.modalvisible;
export default eventmodalSlice.reducer;
I am not sure what causes this error in this specific slice because I have another slice called user slice which looks almost the same as this slice.
import React from "react"
import { createSlice } from "@reduxjs/toolkit"
import { act } from "react-test-renderer";
export const userSlice = createSlice({
name: "user",
initialState:{
user: null
},
reducers: {
Signin: (state, action) => {
state.user = action.payload;
},
Register: (state, action) => {
state.user = action.payload;
},
Signout: (state) => {
state.user = null;
}
}
});
export const { Signin, Register, Signout} = userSlice.actions;
export const selectUser = (state) => state.user.user;
export default userSlice.reducer;
State.user.user does not error in this last snippet. This is what's most confusing since these snippets look pretty much the same unless I am missing something. Any help would be appreciated here.
My store looks like this
import { configureStore } from "@reduxjs/toolkit";
import userReducer from "./userSlice"
import eventmodalReducer from "./eventmodalSlice"
export default configureStore({
reducer: {
user: userReducer,
eventmodal: eventmodalReducer
},
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
