Twitter khuong291
Twitter khuong291

Reputation: 11672

Initial variable is not defined in reducer

I have a todos variable declare here:

const initialState = {
  todos: []
};

export const todo = (state: RootState = initialState, action: Action) => {
  switch (action.type) {
    case TODO_ADD:
      return {
        todos: [...state.todos, action.payload.todo]
      };
    case TODO_TOGGLE_COMPLETE:
      const todos = [...state.todos];
      todos.forEach((todo: ToDo, index: number) => {
        if (todo.id === action.payload.id) {
          todos[index].isComplete = !todos[index].isComplete;
        }
      });
      return {
        todos
      };
    default:
      return state;
  }
};

But don't know why it always say not defined

enter image description here

Upvotes: 1

Views: 202

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074168

Unfortunately, this can happen sometimes with compiled/transpiled code (in your case, code compiled by tsc). Source maps are good, but they're not perfect.

When you run into this, you may have to fall back to debugging the underlying generated JavaScript rather than using the source maps to make it seem like you're debugging your TypeScript code.

Upvotes: 1

Related Questions