Programmatically navigate using react router

后端 未结 30 2295
無奈伤痛
無奈伤痛 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:41

    For ES6 + React components, the following solution worked for me.

    I followed Felippe skinner, but added an end to end solution to help beginners like me.

    Below are the versions I used:

    "react-router": "^2.7.0"

    "react": "^15.3.1"

    Below is my react component where I used programmatic navigation using react-router:

    import React from 'react';
    
    class loginComp extends React.Component {
       constructor( context) {
        super(context);
        this.state = {
          uname: '',
          pwd: ''
        };
      }
    
      redirectToMainPage(){
            this.context.router.replace('/home');
      }
    
      render(){
        return 
    // skipping html code
    ; } }; loginComp.contextTypes = { router: React.PropTypes.object.isRequired } module.exports = loginComp;

    Below is the configuration for my router:

     import { Router, Route, IndexRedirect, browserHistory } from 'react-router'
    
     render(
              
                
                
                
                
                
              
            , document.getElementById('root'));
    

提交回复
热议问题