0

When I try to acess action.payload inside a object show this error: "',' expected", in VSCODE. But, when I try to acess inside a [array] works fine.

export function userReducer(
  state: IState = initialState,
  action: UserActions.Types
) {
  switch (action.type) {
    case UserActions.LOGIN:
      return {
        ...state,
        profile: {
          ...state.profile,
          action.payload // <------------------ does't work
        }
      };
    case UserActions.LOGOUT:
      return {
        ...state,
        profile: [
          ...state.profile,
          action.payload // <------------------ work
        ]
      };
    default:
      return state;
  }
}

Why this hapens? How I could fix?

Thanks.

2 Answers2

0

you should use

payload: action.payload 

inside object.

Vivek Jain
  • 2,730
  • 6
  • 12
  • 27
0

The parts of an object are key:value.

In your example, you are trying to use the action.payload without specifying a key of the object

Try this

profile: {
          ...state.profile,
          ...action.payload
        }

or this

profile: {
          ...state.profile,
          payload: action.payload
        }

It works fine in the array because it's different nature. Meaning that in the array you do not have to specify a key:value. You have just to add items in the array like ['an_item', 1, {foo:bar}]

profanis
  • 2,741
  • 3
  • 39
  • 49