react利用 js (点击等)触发控制路由跳转

2019-05-15  本文已影响0人  Dr丶net

利用 react-router的withRouter进行 js 控制路由跳转

import React,{Component} from 'react';
import { withRouter } from 'react-router';
@withRouter
export default myComponent extends Component {
    constructor(props){
        super(props)
    }
    routerChange(){
        const { history } = this.props;
        history.push('你要跳转的路由')
    }
    render(){
        return(
            <div>
                <button 
                    onClick={
                        this.routerChange.bind(this)
                    } 
                >
                点我跳转
                </button>
            </div>
        )
    }
}

PS:上述代码使用了 babel 装饰器方法,相关配置方法请点击下方链接

creat-react-app Ver2.0+配置装饰器语法

上一篇下一篇

猜你喜欢

热点阅读