重定向在reactJs中不起作用

时间:2017-05-26 10:37:05

标签: reactjs

这是我的index.js页面

import React from 'react';
import ReactDOM from 'react-dom';
import Login from './Login';
import Dashboard from './Dashboard';
import { BrowserRouter as Router, Route} from 'react-router-dom';
import './css/bootstrap.min.css';
import './css/font-awesome.min.css';
import './css/style.css';
import { createHistory, useBasename } from 'history'

const history = useBasename(createHistory)({
    basename: '/'
})   

ReactDOM.render((
  <Router history={history}>
    <div>
       <Route   path="/" component={Login} />
       <Route path="dashboard" component={Dashboard} store={Dashboard} />
       <Route exact path="login" component={Login} store={Login} />
    </div>
  </Router>
),
  document.getElementById('root')
);

这是我的登录页面。在点击按钮时,它不会重定向到相应的组件。

import React, { Component } from 'react';


export default class Login extends Component {
constructor (props){
        super(props);
        this.state = {
            email : '',
            password : '',
            userId : ''
        };
    }

    login(){
      //this.props.router.push('/dashboard'); // Its was not working
      this.props.history.push('dashboard'); //Its working for me
    }
 render() {
    return (
    <div className="container-fluid">
    <div className="row">
        <div className="col-xl-12">    
            <div className="login-page-block-inner">
                <div className="login-page-block-form">
                        <div className="form-actions">
                            <button type="button"  className="btn btn-primary width-150" onClick={(e) => { this.login()} }>Sign In</button>
                        </div>                       
                </div>
            </div>
        </div>
    </div>
</div>
    );
  }
}

请帮我找出解决此问题的方法。我很担心这个。

3 个答案:

答案 0 :(得分:1)

利用withRouter frun react-router将路由器作为支持注入您的登录组件

import React, { Component } from 'react';
import {withRouter} from 'react-router'
import $ from 'jquery';

class Login extends Component {
constructor (props){
        super(props);
        this.state = {
            email : '',
            password : '',
            userId : ''
        };
    }

    login(){
      this.props.history.push('/dashboard');
    }
 render() {
    return (
    <div className="container-fluid">
    <div className="row">
        <div className="col-xl-12">    
            <div className="login-page-block-inner">
                <div className="login-page-block-form">
                        <div className="form-actions">
                            <button type="button"  className="btn btn-primary width-150" onClick={(e) => { this.login()} }>Sign In</button>
                        </div>                       
                </div>
            </div>
        </div>
    </div>
</div>
    );
  }
}
export default withRouter(Login)

答案 1 :(得分:1)

在React 16及更高版本中,您可以从'react-router-dom'使用重定向

import  { Redirect } from 'react-router-dom' 

在组件中定义状态

this.state = {
  loginStatus:true
}

比渲染方法中的

render () {
if(this.state.loginStatus){
    return <Redirect to='/home'  />
 }
return(
 <div> Please Login </div>
 )
}

答案 2 :(得分:0)

在您的情况下,this.props.router是不确定的。这是我提出的一个粗略解决方案。阅读代码中的注释将有所帮助。

import React, { Component } from 'react';
import { Redirect } from 'react-router-dom'; // add this import 

export default class Login extends Component {
constructor (props){
        super(props);
        this.state = {
            email : '',
            password : '',
            userId : '',
            redirectToReferrer: true // when you're checking if the user is authenticated you have to keep this false
        };
    }
 //    componentWillReceiveProps(nextProps) {
 //    if ( put your authentication logic here ) {
    //       this.setState({ redirectToReferrer: true });
    //     }
    // }

    login(){
      this.props.history.push('/dashboard');
    }
 render() {
    const from = { pathname: '/dashboard' };
    const { redirectToReferrer } = this.state; // redirectToReferrer is true in the initial state
    if (redirectToReferrer) { // if true the user will be redirected to /dashboard
      return <Redirect to={from} />;
    }
    return (
    <div className="container-fluid">
    <div className="row">
        <div className="col-xl-12">    
            <div className="login-page-block-inner">
                <div className="login-page-block-form">
                        <div className="form-actions">
                            <button type="button"  className="btn btn-primary width-150" onClick={(e) => { this.login()} }>Sign In</button>
                        </div>                       
                </div>
            </div>
        </div>
    </div>
</div>
    );
  }
}