Skip to content
Advertisement

TypeError: Cannot read properties of undefined (reading ‘roomId’)

I’m trying to get the roomId from firestore but I’m stuck with this problem since last week I have tried different es6 syntax to overcome with but no result. Below I have provided the code it shows the error on selectRoomId

import { createSlice } from '@reduxjs/toolkit';

export const appSlice = createSlice({
  name: 'app',
  initialState: {
    roomId: null
  },
  reducers: {
    enterRoom: (state, action) => {
      state.roomId = action.payload.roomId;
    },
  },
});

export const { enterRoom } = appSlice.actions;
export const selectRoomId = state => state.app.roomId;
export default appSlice.reducer;

Advertisement

Answer

Spread your state and update the roomId only

reducers: {
  enterRoom: (state, action) => {
    ...state,
    roomId: action?.payload?.roomId;
  },
},
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement