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.

History push doesn't render navigated page

See original GitHub issue

I got the following Router. I want to navigate to a new page. For this I tried <Redirect> first and this.props.history.push("/login") later.

In both cases the URL gets updated in the browser, but the page doesn’t rerender.

I’m using redux, redux-actions, redux-saga, react-router, react-router-dom, react, redux-form, react-dom

Example component

 export namespace Hom {
  export interface Props extends RouteComponentProps<void> {
    authenticated: boolean;
  }

  export interface State {}
}

//@connect(mapStateToProps, mapDispatchToProps)
class Hom extends React.Component<any, any> {
  // this function is bound
  goLogin = () => {
      return (
        <Redirect to="/login" />
      )
  }

  goLogin2 = () => {
    this.context.router.transition('/login');
  }

  componentWillMount() {
    if (!this.props.authenticated) {
      this.props.history.push('/login');
    }
  }

  render() {
    const { children, authenticated } = this.props;

    return (
      <div id="home">
        { authenticated && <div>
          <Header />
            <Row>
              <News /> 
              <LastSymbols />
            </Row>
          </div> }
      </div>
    );
  }
}

function mapStateToProps(state: RootState) {
  return {
    authenticated: state.auth.authenticated
  };
}

function mapDispatchToProps(dispatch) {
  return {};
}

export const Home = withRouter(connect<any, any, any>(mapStateToProps, mapDispatchToProps)(Hom))
ReactDOM.render(
  <Provider store={store}>
    <BrowserRouter>
      <App>
        <Switch>
          <Route exact path="/" component={Home} />
          <Route path="/home" component={Home} />
          <Route path="/stock/:name" component={Stock} />
          <Route path="/login" component={Login} />
        </Switch>
      </App>
    </BrowserRouter>
  </Provider>,
  document.getElementById('root')
);

Version

newest

Steps to reproduce

apply code above

Expected Behavior

render new page

Actual Behavior

doesn’t render new page

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

1reaction
nihiluiscommented, Jun 4, 2017

solution: removed `@connect(mapStateToProps, mapDispatchToProps) on App

0reactions
risaWYcommented, Nov 13, 2018

@gr8pathik Nope. I believe I just did something else and avoided having to do this. Please post your solution here if you find it. Sorry, and thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component is not getting rendered after history.push()
You'll import this history instance and navigate with history.push('/'); and so on. Working example: https://codesandbox.io/s/5ymj657k1k.
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 - 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 >
Migrating to React Router v6: A complete guide
Migrate your React Router applications from v5 to v6 with this in-depth guide, including a review of additions and improvements from v5.
Read more >
Programmatically Navigate with React Router - Telerik
history.push() is another approach where we make use of the history props React Router provides while rendering a component.
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