Programmatically navigate using react router

后端 未结 30 2369
無奈伤痛
無奈伤痛 2020-11-21 05:18

With react-router I can use the Link element to create links which are natively handled by react router.

I see internally it calls t

30条回答
  •  忘了有多久
    2020-11-21 05:27

    React-Router v2

    For the most recent release (v2.0.0-rc5), the recommended navigation method is by directly pushing onto the history singleton. You can see that in action in the Navigating outside of Components doc.

    Relevant excerpt:

    import { browserHistory } from 'react-router';
    browserHistory.push('/some/path');
    

    If using the newer react-router API, you need to make use of the history from this.props when inside of components so:

    this.props.history.push('/some/path');
    

    It also offers pushState but that is deprecated per logged warnings.

    If using react-router-redux, it offers a push function you can dispatch like so:

    import { push } from 'react-router-redux';
    this.props.dispatch(push('/some/path'));
    

    However this may be only used to change the URL, not to actually navigate to the page.

提交回复
热议问题