Search code examples
reactjsreduxredux-toolkit

Redux Uncaught TypeError: state is undefined


I am trying to add Redux-toolkit to my application but receiving the following error.

Uncaught TypeError: state.treatment is undefined

The offending line is when I use useAppSelector within a component:

import { useAppDispatch, useAppSelector } from '../reduxHooks';
import { setName } from '../store/treatmentSlice';

const TreatmentsGrid = () => {
  const patientName = useAppSelector((state) => state.treatment.patientName);
  // ERROR: Uncaught TypeError: state.treatment is undefined
  const dispatch = useAppDispatch();

Here is how I have setup Redux toolkit according to the Redux toolkit TS documentation

store.ts

import { configureStore } from '@reduxjs/toolkit';
import treatmentReducer from './treatmentSlice';

export const store = configureStore({
  reducer: {
    treatment: treatmentReducer,
  },
});

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>

export type AppDispatch = typeof store.dispatch;

treatmentSlice.ts

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

// Define a type for the slice state
interface TreatmentState {
  patientName: string,
}

// Define the initial state using that type
const initialState: TreatmentState = {
  patientName: 'User',
};

export const treatmentSlice = createSlice({
  name: 'treatment',
  initialState,
  reducers: {
    setName: (state, action: PayloadAction<string>) => {
      console.log(action.payload);
      // eslint-disable-next-line no-param-reassign
      state.patientName = action.payload;
    },
  },
});

// Extract the action creators object and the reducer
export const { setName } = treatmentSlice.actions;

export default treatmentSlice.reducer;

reduxHooks.ts

import { useDispatch, useSelector } from 'react-redux';
import type { TypedUseSelectorHook } from 'react-redux';
import { AppDispatch, RootState } from './store/store';

// Use throughout your app instead of plain `useDispatch` and `useSelector`
export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

index.tsx

import { store } from './store/store';
import { Provider } from 'react-redux';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement,
);
root.render(
  <AuthContextProvider>
    <React.StrictMode>
      <Provider store={store}>
        <App />
      </Provider>
    </React.StrictMode>
  </AuthContextProvider>,
);

package.json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@date-io/date-fns": "^2.15.0",
    "@emotion/react": "^11.10.4",
    "@emotion/styled": "^11.10.4",
    "@mui/icons-material": "^5.10.3",
    "@mui/material": "^5.10.3",
    "@mui/x-data-grid": "^5.17.0",
    "@mui/x-date-pickers": "^5.0.0",
    "@reduxjs/toolkit": "^1.8.5",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^14.4.3",
    "@types/jest": "^29.0.0",
    "@types/node": "^18.7.14",
    "@types/react": "^18.0.18",
    "@types/react-data-grid": "^4.0.8",
    "@types/react-dom": "^18.0.6",
    "@types/react-router-dom": "^5.3.3",
    "axios": "^0.27.2",
    "date-fns": "^2.29.2",
    "filepond": "^4.30.4",
    "jwt-decode": "^3.1.2",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-filepond": "^7.1.2",
    "react-redux": "^8.0.2",
    "react-router-dom": "^6.3.0",
    "react-scripts": "5.0.1",
    "typescript": "^4.8.2",
    "web-vitals": "^3.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@redux-devtools/core": "^3.13.1",
    "@typescript-eslint/eslint-plugin": "^5.36.1",
    "@typescript-eslint/parser": "^5.36.1",
    "eslint": "8.22.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-jsx-a11y": "^6.6.1",
    "eslint-plugin-prefer-arrow": "^1.2.3",
    "eslint-plugin-react": "^7.31.2"
  }
}

I am not sure how to fix this error, I have followed the docs to the best of my ability.


Solution

  • Apologies, this was due to a stupid mistake on my part. Leaving this answer here if someone else makes a similar mistake...

    I was also using RTK Query's <ApiProvider /> component within my App.tsx. This was overwriting the redux store I was expecting, thus making state.treatments undefined.

    I have resolved this by incorporating the RTK Query slice into my existing store.

    export const store = configureStore({
      reducer: {
        [treatmentsApi.reducerPath]: treatmentsApi.reducer,
        treatment: treatmentReducer,
      },
    });