Programmatically navigate using react router

后端 未结 30 2443
無奈伤痛
無奈伤痛 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条回答
  •  Happy的楠姐
    2020-11-21 05:22

    With the current React version (15.3), this.props.history.push('/location'); worked for me, but it showed the following warning:

    browser.js:49 Warning: [react-router] props.history and context.history are deprecated. Please use context.router.

    and I solved it using context.router like this:

    import React from 'react';
    
    class MyComponent extends React.Component {
    
        constructor(props) {
            super(props);
            this.backPressed = this.backPressed.bind(this);
        }
    
        backPressed() {
            this.context.router.push('/back-location');
        }
    
        ...
    }
    
    MyComponent.contextTypes = {
        router: React.PropTypes.object.isRequired
    };
    
    export default MyComponent;
    

提交回复
热议问题