Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Location state absent in LOCATION_CHANGE action #301

Closed
fzaninotto opened this issue Apr 30, 2019 · 3 comments · Fixed by #302
Closed

Location state absent in LOCATION_CHANGE action #301

fzaninotto opened this issue Apr 30, 2019 · 3 comments · Fixed by #302

Comments

@fzaninotto
Copy link
Contributor

React-router uses history's location object:

The location object implements a subset of the window.location interface, including:

  • location.pathname - The path of the URL
  • location.search - The URL query string
  • location.hash - The URL hash fragment

Locations may also have the following properties:

  • location.state - Some extra state for this location that does not reside in the URL (supported in createBrowserHistory and createMemoryHistory)
  • location.key - A unique string representing this location (supported in createBrowserHistory and createMemoryHistory)

(see https://github.com/ReactTraining/history#listening)

connected-react-router only passes pathname, search and hash fields to the LOCATION_CHANGE action, and ignores the state and key fields.

const {
pathname: pathnameInStore,
search: searchInStore,
hash: hashInStore,
} = getLocation(store.getState())
// Extract history's location
const {
pathname: pathnameInHistory,
search: searchInHistory,
hash: hashInHistory,
} = history.location
// If we do time travelling, the location in store is changed but location in history is not changed
if (pathnameInHistory !== pathnameInStore || searchInHistory !== searchInStore || hashInHistory !== hashInStore) {
this.inTimeTravelling = true
// Update history's location to match store's location
history.push({
pathname: pathnameInStore,
search: searchInStore,
hash: hashInStore,
})
}

As a consequence, a link using location state like this:

<Link to={{ pathname: '/hello', state: { foo: 'bar' } }}>Hello</Link>

Is translated by connected-react-router as the following action:

{
  type: '@@router/LOCATION_CHANGE',
  payload: {
    location: {
      pathname: '/hello',
      search: '',
      hash: ''
    },
    action: 'POP',
    isFirstRendering: false
  }
}

Notice the missing state key in the location.

As a consequence, reducers can't use the value passed in the state.

Note that this was properly handled by react-router-redux.

@nefil1m
Copy link

nefil1m commented Jul 16, 2019

@supasate any update on this? It looks like the fix is pretty simple and I'm not sure why the issue is stale for so long

@rob2d
Copy link

rob2d commented Dec 5, 2019

Update? @fzaninotto has a solution in his PR which looks pretty intuitive. Right now, otherwise, requiring a bit of over-engineering to use route params within reducers or actions which isn't great (especially for cross-OS/react native systems). This is about 65% of the reason I just added connected-react-router (though in fairness I should've done my research before choosing... but adding this would be very nice/empathetic on the part of the maintainers 😛 ).

@digicontributer
Copy link

Please review this.

fzaninotto added a commit to fzaninotto/connected-react-router that referenced this issue Jan 27, 2020
fzaninotto added a commit to fzaninotto/connected-react-router that referenced this issue Jan 27, 2020
supasate pushed a commit that referenced this issue Jan 29, 2020
* Include location state and key in LOCATION_CHANGE payload

Closes #301

* Remove location.key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants