0

So, i am trying to setup redux in my project. And there is very strange problem showing up in reducer: Uncaught TypeError: Cannot read properties of undefined (reading 'type'). Here is reducer.ts:

import { MarkerActionEnum, MarkerActions, MarkerState } from './types';

const initialState: MarkerState = {
  data: undefined,
};

export function MarkerReducer(
  action: MarkerActions,
  state = initialState,
): MarkerState {
  switch (action.type) {
    case MarkerActionEnum.ADD_MARKER:
      return {
        ...state,
        data: action.payload,
      };
    case MarkerActionEnum.DELETE_MARKER: {
      return { data: undefined };
    }
    default:
      return state;
  }
}

export default MarkerReducer;

and types.ts for reducer:

import { Action } from 'redux';
import { MarkerType } from '../../pages/mapPage/components/CustomMap';

export interface MarkerState {
  data: MarkerType | undefined;
}

export enum MarkerActionEnum {
  ADD_MARKER = 'marker/ADD_MARKER',
  DELETE_MARKER = 'marker/DELETE_MARKER',
}

export interface AddMarkerInterface
  extends Action<MarkerActionEnum.ADD_MARKER> {
  type: MarkerActionEnum.ADD_MARKER;
  payload: { longitude: number; latitude: number };
}

export interface DeleteMarkerInterface
  extends Action<MarkerActionEnum.DELETE_MARKER> {
  type: MarkerActionEnum.DELETE_MARKER;
}

export type MarkerActions = AddMarkerInterface | DeleteMarkerInterface;

Log says that the problem occurs in line 11 of reducer, right in switch (action.type) Does anyone know or stumbled in something like this?

1
  • As a side note: it looks like you're may be using some legacy-style patterns for Redux+React-Redux. "Modern Redux" with Redux Toolkit and the React-Redux hooks API should help simplify your code and be easier to work with, especially if you're using TS. (Like, that entire separate "reducer types" file would go away completely!) See our Redux docs tutorials for details: redux-toolkit.js.org/tutorials/overview , as well as redux.js.org/usage/… . Commented May 21, 2022 at 22:32

2 Answers 2

3

I had the same issue. The problem is the ordering of the parameters. "State" should be the first and "action" the second.

Sign up to request clarification or add additional context in comments.

Comments

0

I'd say it means you have used as argument to dispatch something that is not an action, i.e. has no type property. For example if you have an action builder and forgot the parentheses, like in dispatch(myActionBuilder) instead of dispatch(myActionBuilder()).

2 Comments

but it still should return default state isn't it?
No, because it throws an error at switch (action.type), so it never reaches the return statement. Just console.log(action) before that point and you will understand where the problem comes from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.