Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当执行IF条件时,如何重定向到ReactJS中的另一个页面?_Javascript_Reactjs_Ecmascript 6_React Router - Fatal编程技术网

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

Javascript 当执行IF条件时,如何重定向到ReactJS中的另一个页面?,javascript,reactjs,ecmascript-6,react-router,Javascript,Reactjs,Ecmascript 6,React Router,如果用户名和密码匹配,我想重定向到Dashboard.jsx。怎么做?我是新手 在If条件中,我想添加重定向代码来重定向另一个页面。 请回复。在stackoverflow中,最大值是在没有if条件的情况下使用的,所以这里是区别 var users={ name:'bddebashis', password:'debashis111249' } class Home extends Component { constructor() { super(); this.handl

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

在If条件中,我想添加重定向代码来重定向另一个页面。 请回复。在stackoverflow中,最大值是在没有if条件的情况下使用的,所以这里是区别

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';/>

    }
  }
var用户={
名称:'bddebashis',
密码:'debashis111249'
}
类Home扩展组件{
构造函数(){
超级();
this.handleSubmit=this.handleSubmit.bind(this);
}
handleSubmit(事件){
event.preventDefault();
const data=新表单数据(event.target);
获取(“/api/form submit url”{
方法:“POST”,
正文:数据,
});
if(data.get('usr')==users.name&&data.get('paswd')==users.password){
}
}

如果您使用的是React路由器,则可以使用重定向组件



在渲染函数中添加重定向组件应该足够了。

重定向通过使用
历史记录
模块使操作变得更简单。安装历史记录模块
npm安装历史记录
,然后像这样配置添加路由器。
// 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")  
        }
      }
    }
AppRouter.js

import { Router, Route, Switch } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
export const history = createHistory();

<Router history={history}>
   <Route path="/about" component={AboutPage} />
   <Route ... />
   ...
<Router>

从重定向中删除点,它应该是
到='/Dashboard'
,您的问题已经在这里得到了解答:可能重复了非常感谢您的内容,您节省了我的时间。
import { Router, Route, Switch } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
export const history = createHistory();

<Router history={history}>
   <Route path="/about" component={AboutPage} />
   <Route ... />
   ...
<Router>
import {history} from './AppRouter';

history.push('/dashboard');
import createHistory from 'history/createBrowserHistory';
export const history = createHistory();

<Router history={history}>
   <Route />

   <Router>
history.push('/Dashboard');