-
Notifications
You must be signed in to change notification settings - Fork 960
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
Page is not rendering after history.push('/path') #822
Comments
I'm running into the same. Here's a self contained example: https://codesandbox.io/embed/crazy-dirac-04nil?fontsize=14&hidenavigation=1&theme=dark import React from 'react'
import {Router} from 'react-router'
import {Route} from 'react-router-dom'
import {createBrowserHistory} from 'history'
const myHistory = createBrowserHistory()
export default function App() {
return (
<Router history={myHistory}>
<Route
path="/"
exact={true}
render={() => <h1 onClick={() => myHistory.push('/todos')}>Home</h1>}
/>
<Route path="/todos" exact={true} render={() => <h1>Todos</h1>} />
</Router>
)
} Downgrading this example to |
I have the same
You made my entire day :) |
I have the same, it only works with me if I downgrade to the |
I am seeing the same issue with v5 using
when I rolled back to |
Same problem! |
Writing this here to document what I found in more detail, not sure if it is redundant or not. I ran into this issue and tried to do a bit of debugging inside the It seems that with For example, context.location = {
pathname,
search,
hash,
state,
key,
} Becomes context.location = {
action: 'REPLACE',
location: {
pathname,
search,
hash,
state,
key,
}
} (found from logging the context in Since the I assume this is being tackled in react-router v6, and I don't know if it makes sense for this stage to update v5 to play nicely with this breaking change on |
FYI, I posted a more detailed overview of all the breaking changes I could find in #811. |
@StringEpsilon Thanks for the list, looks like important info for users. I may be missing something but is the change I mentioned listed there already? In case it isn't, I think it would be a good addition, because it breaks a pretty common use-case. |
It's the history.listen() change. You can see more details on most entries in the gist. |
With history 5.x.x, Fails when using
Rendering works nicely when using BrowserRouter.
|
@mayur-novus That's because using |
Thank you so much! I was downgrade my 'hisory' from 5.0.0 to 4.1.0 as you mentioned, and now everything works properly ) |
Alternatively, here's a solution that does not require downgrading Under Here's an example: // polyfill CustomEvent
import CustomEvent from "custom-event"
import { useEffect } from "react"
import { render } from "react-dom"
import { useHistory } from "react-router-dom"
// Dispatch a history event.
const dispatchHistoryEvent = (action, route, state) => {
const event = new CustomEvent("history", {
detail: {
action,
route,
state
}
})
document.dispatchEvent(event)
}
// Execute a history event.
const handleHistoryEvent = (history, event) => {
const { action, route, state } = event.detail
history[action](route, state)
}
// Hook up history event listener that will execute history events.
const useHistoryEvent = () => {
const history = useHistory()
useEffect(() => {
const handler = e => handleHistoryEvent(history, e)
document.addEventListener("history", handler)
return () => document.removeEventListener("history", handler)
}, [])
}
const App = ()=> {
useHistoryEvent()
// ...
}
render(
<BrowserRouter>
<App>
{/* routes */}
</App>
</BrowserRouter>,
document.getElementById("root")
)
// somewhere outside of component tree
dispatchHistoryEvent("push", "/home/", {/* optional state */}) Note that this works for all |
Hi,
I have a problem using history v. 5 with react-router-dom. When I call function history.push('/cart') it sends me to the correct page, but it doesn't show the content.
If I use history version: "4.10.1", it works perfectly.
Am I doing something wrong ?
This is my history.js file
And this is my app.js
The text was updated successfully, but these errors were encountered: