Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Calling routerReducer() with no args crashes #349

Closed
@mindjuice

Description

@mindjuice

If you call routerReducer() with no arguments to get back the initial state, it crashes with the following error:

reducer.js: Uncaught TypeError: Cannot read property 'type' of undefined

The generated code looks like this:

function routerReducer() {
  var state = arguments.length <= 0 || arguments[0] === undefined ? initialState : arguments[0];
  var _ref = arguments[1];
  var type = _ref.type;       // <==== _ref is undefined here and _ref.type causes the error
  var payload = _ref.payload;

  if (type === LOCATION_CHANGE) {
    return _extends({}, state, { locationBeforeTransitions: payload });
  }

  return state;
}

This can be fixed in the original code by adding a default parameter for the action parameter:

export function routerReducer(state = initialState, { type, payload } = {}) {

It made me wonder though what the correct behavior of Babel should be here. Should it provide safe destructuring or still leave that up to your code? I checked Traceur though and it behaves the same way, so perhaps that's not part of the spec, but it seems dangerous.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions