-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Unexpected difference between clicking a <Link> or clicking the back button #5072
Comments
@deiucanta You can safely ignore this comment. @timdorr I went ahead and made a |
@pshrmn I had created Projects for all of them, but they aren't really that easy to use I've found. The labels work better. @deiucanta That appears to be a side effect of using that site. I guess their browser emulation isn't complete or to spec. |
@timdorr I initially got this behaviour working a project on my local machine and then I replicated it in codesandbox.io. Please reopen the issue and let me know if you have other hints on where the issue might be. |
Getting the same behavior after downloading and running it locally. I looked into it a bit, and I think the main problem is that, while both The reason it works on the first transition (clicking Page x) is that React batches the first update since the update results from an event handled by React, so When appending |
@taurose Inspecting with ReactDevTools I can see that The component structure is like this
On a regular location change, this is what happens
So, you're basically saying that step 3 somehow happens before step 2 and when the Am I right? Should this be fixed in the library or should I change my code to make it work? |
@taurose Thanks for the investigation. This sounds like a legit bug with the library. @deiucanta Sorry for the fast close. It's still in alpha, so things like this need to be worked out. |
Hey guys - can confirm another case of this... I've noticed the location object within the |
@deiucanta Yep, that sums it up well. By the way, I think it should work fine with controlled |
I think I was having this same problem and "solved" this for myself by connecting my switches to the store. Or you could pass the router location down through its props manually (since Switch allows you to pass in your own location object).
If you look in the React developer tools (at your switch component), you can actually see where the Switch component hangs up on the old location object without this hack. |
@pmwisdom Came to same solution as well. However, to me it feels like Switches wrapped in |
@iljadaderko totally agree |
@timdorr did you manage to look into this? If you give me some guidance I'm happy to make a PR. |
I think I have the same problem. I use I added logging statements to The child context is updated with the new location and all When using the back button, however, it looks differently: All The location is set correctly in the Store, by the way, so the navigation highlights I do not really have an idea, yet, why the order is different. It is also interesting Why does it help to connect
|
FYI: I have been having the same issue but with react-router-redux v5 and react-router-dom. The suggestion from @pmwisdom to create a |
Experiencing this as well. Using ‘react-router-redux’, the As previously mentioned, in the mean time passing down location from the connectedRouter as a prop to |
@Geoff-Ford, Can you set up a gist with your |
|
Thanks @deiucanta. After I posted that question, I did find this (which is similar). |
Sorry for the tarde response. What @deiucanta has provided there is exactly the same as I have implemented too. |
I wonder if #5203 fixed this unintentionally... |
I hit this too on a set of Routes outside a switch. So I guess it means a ConnectedRoute is needed if there isn't a cleaner fix. Just goes to show why context is not recommended by the react team! |
My 50 cents. Do not send location to Switch: import React from 'react'
import { connect } from 'react-redux'
import { Switch, Route } from 'react-router-dom'
const addLocation = connect(state => ({
location: state.routing.location,
}))
export const ConnectedSwitch = addLocation(({ location, ...props }) => <Switch {...props} />)
export const ConnectedRoute = addLocation(props => <Route {...props} />) |
Thanks @pmwisdom! I thought I was going nuts! I ended up using method 2, passing location directly to Switch. It appeared to work ok, until in my case I realized that each route page was undergoing an FULL rerender on updates to the store for some strange reason, and this really wreaked havoc. Instead of connecting Switch, I ended up connecting each individual page component instead. Not ideal, but at least the rendering is working as expected now (efficient partial DOM updates). Not sure if I have stuff setup wrong.. wouldn't be surprised... |
I have came across this library and article (from Facebook docs recommendation) I have a question: Have react-router started using this observable pattern yet? |
@timdorr this may be because of the
fixes the issue. Example This is also mentioned in https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md and https://reacttraining.com/react-router/core/guides/redux-integration/blocked-updates |
I wonder if we should be creating the ConnectedSwitch everytime we want to provide a Switch in order to not to mess up with the connected location state or open up a pull request so React Router Redux can provide it's own ConnectedSwitch... |
Another related bug : https://codesandbox.io/s/z6ozxy053 As for the ConnectedSwitch, I believe it would work, but I am also using react-router-config, which uses Switch internally in renderRoutes, and I don't know of a way to pass your own location object to the underlying switch in ConnectedSwitch. |
Closing this out since react-router-redux is deprecated. Use connected-react-router instead! |
Test Case
https://codesandbox.io/s/G6nWE3X0r
Steps to reproduce
Expected Behavior
It should go back to
/
.Actual Behavior
It's stuck showing "page 2".
The
<App>
component is not blocking the update since it has the 'location' injected via theconnect()
function. However, if I changeconnect()
towithRouter()
it works.Also, when I remove the
<Switch>
component the back button works as expected.The text was updated successfully, but these errors were encountered: