如何在执行IF条件时重定向到ReactJS中的另一个页面?

Bas*_*his 2 javascript ecmascript-6 reactjs react-router

如果用户名和密码匹配,我想重定向到Dashboard.jsx.怎么做 ?我是ReactJS的新手.

在If条件中我想添加重定向代码以重定向另一个页面.请回应.在stackoverflow中,最大值是在没有条件的情况下使用,所以这里是差异.

var users={
name:'bddebashis',
password:'debashis111249'
}

class Home extends Component {


constructor() {
    super();
    this.handleSubmit = this.handleSubmit.bind(this);
}


 handleSubmit(event) {
    event.preventDefault();
    const data = new FormData(event.target);

    fetch('/api/form-submit-url', {
        method: 'POST',
        body: data,
    });

    if(data.get('usr')==users.name && data.get('paswd')==users.password){
      <Redirect to='./Dashboard';/>

    }
  }
Run Code Online (Sandbox Code Playgroud)

Pra*_*bhu 5

// Are You using BrowserRouter means you can use like this

import PropTypes from 'prop-types';

var users={
    name:'bddebashis',
    password:'debashis111249'
    }

    class Home extends Component {


    constructor() {
        super();
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    static contextTypes = {
        router: PropTypes.object,
      }

     handleSubmit(event) {
        event.preventDefault();
        const data = new FormData(event.target);

        fetch('/api/form-submit-url', {
            method: 'POST',
            body: data,
        });

        if(data.get('usr')==users.name && data.get('paswd')==users.password){
          this.context.router.history.push("/Dashboard")  
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)