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

Using <Switch /> makes location.state accessed from withRouter HOC to not be defined. #7656

Closed
npetridis opened this issue Oct 14, 2020 · 1 comment

Comments

@npetridis
Copy link

Version

In my environment: 4.3.1
In sandbox provided: 5.2.0

Test Case

https://codesandbox.io/s/react-router-forked-vd9kr?file=/index.js

Steps to reproduce

  1. press the Home link to go to /test page
  2. In the comment at the sandbox when using Switch inside a nested route access to location.state from withRouter is overwritten. When not (use Fragment at the sandbox) then it works as expected.

Expected Behavior

When using Switch and I access location.state props from withRouter HOC I can see the state I have passed to the specific route.
Screenshot 2020-10-14 at 3 39 34 PM

Actual Behavior

When using Switch and I access location.state props from withRouter HOC the state not defined.
Screenshot 2020-10-14 at 3 39 47 PM

@npetridis npetridis changed the title Using <Switch /> makes access to location.state from withRouter HOC it is not defined. Using <Switch /> makes location.state accessed from withRouter HOC to not be defined. Oct 14, 2020
@timdorr
Copy link
Member

timdorr commented Oct 14, 2020

Switch sets the location prop on the child it selects to render. This isn't the correct way to set that state anyways. You should be setting it on the Link so it's saved in the history stack.

@timdorr timdorr closed this as completed Oct 14, 2020
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

No branches or pull requests

2 participants