question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Page is not rendering after history.push('/path')

See original GitHub issue

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

import { createBrowserHistory } from 'history';
const history = createBrowserHistory();
export default history;

And this is my app.js

import React from 'react';
// import { Router } from 'react-router-dom';
import { Router } from 'react-router';
import { Provider } from 'react-redux';
import { ToastContainer } from 'react-toastify';
import store from './store/index';
import history from './services/history';

import Routes from './routes';
import GlobalStyle from './styles/global';
import Header from './components/Header';

function App() {
	return (
		<Provider store={store}>
			<Router history={history} >
				<Header />
				<Routes />
				<GlobalStyle />
				<ToastContainer autoClose={3000} />
			</Router>
		</Provider>
	);
}

export default App;

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:27
  • Comments:13

github_iconTop GitHub Comments

24reactions
ritchcommented, Jul 21, 2020

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 history@4.10.1 fixes the issue.

8reactions
mattfrancis888commented, Jul 21, 2020

I have the same

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 history@4.10.1 fixes the issue.

You made my entire day 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component is not getting rendered after history.push()
components/Home.js (a component like Home is aware of routing and has the history object already passed in via the <Route path="/" ...
Read more >
history.push not rendering component in Router #7415 - GitHub
I experienced same issue now. The URL changed to the supposed history.push("/myURL"), however, the page is not rendering the component. Any ...
Read more >
history.push is only changing the URL, not rendering component
push or props.history.push from any component, you may face the issue of not rendering the component while changing the URL. You can fix...
Read more >
History - React Router: Declarative Routing for React.js
The history object is mutable. Therefore it is recommended to access the location from the render props of <Route> , not from history.location...
Read more >
props.history.push is updating url but not re rendering - Reddit
Check if the situation will change with using `useHistory` hook, you won't be needing the withRouter HOC. Recheck if you don't have nested ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found