javascript - 使用 react 路由器重定向

标签 javascript reactjs redirect react-router

我正在使用 ReactJS 并且有一个表单(组件),如果发布请求成功,则需要重定向到另一个组件。我刚刚开始使用 react 路由器,所以这就是我尝试重定向的方式。

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Redirect, Link } from 'react-router-dom';
import NextComponent from '/NextComponent';
import axios from 'axios';

class CurrentComponent extends Component {

constructor() {
    super();
    this.state = {
        value: ''
        redirect: false
    };
}

handleSubmit() {
    axios.post('xxx/yyy', {
        xxx: yyy
    })
    .then(function() {
        console.log('Success');
        this.setState({redirect: true});
    })
    .catch(function() {
        console.log('Error');
    });
}

render() {
    return(
        <div>
         <form>
          <div className="form-group">
           <input type="name" placeholder="name" />
          </div>
          <div className="form-group">
          <button type="button" onClick={this.handleSubmit.bind(this)} >Submit</button>
          {this.state.redirect &&
           <Redirect to={{
            pathname: '/nextcomponent',
            state: {from: this.state.value}
            }} />
          }
       </div>
         <Router>
           <Route path="/nextcomponent" component={NextComponent} />
         </Router>
        </form>
       </div>
    );
 }
}

export default PresentComponent;

它不是重定向,我一直在试图解决这个问题。我确信有更好的解决方案可用,但由于我缺乏知识,我不确定是否实现它。任何帮助表示赞赏。谢谢。

最佳答案

您可能在调用后没有获得state,请尝试将handleSubmit方法修改为:

handleSubmit() {
    let _this = this;
    axios.post('xxx/yyy', {
        xxx: yyy
    })
    .then(function() {
        console.log('Success');
        _this.setState({redirect: true});
    })
    .catch(function() {
        console.log('Error');
    });
}

根据新代码更新:

class ComponentOne extends Component {

    constructor() {
        super();
        this.UpdateRoute= this.UpdateRoute.bind(this);
        this.state = {
            value: ''
        };
        this.loggedin = false;
    }

    const UpdateRoute = () => (
        <Router>
        <Route exact path="/xyz" render={() => (
          loggedin ? (
            <ComponentTwo />
          ) : (
            <ComponentOne />
          )
        )}/>
        </Router>
    )

    handleSubmit() {
        let _this = this;
        axios.post('/xyz', {
            xxx: yyy,
        })
        .then(function(response) {
            _this.loggedin = true;
            _this.UpdateRoute();
        })
        .catch(function() {
            console.log('Error');
        });
    }

    render() {
        return(
              <div>
            <h1>Load First</h1>
            <button type="button" onClick={this.handleSubmit.bind(this)}>Submit</button>
              </div>
        );
    }
}

export default ComponentOne;

关于javascript - 使用 react 路由器重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44118263/

相关文章:

javascript - 在 for 循环中从数组中删除项目的问题

javascript - 什么意思 return { someObject : someObject }

javascript - 无法使用 PHP 在 jQuery AJAX 中触发错误回调

javascript - React Router 服务器端渲染 Rails

javascript - 当 react js 中的任何状态发生变化时,如何停止重新渲染子组件?

JavaScript - 传单,添加一堆标记

javascript - 如何在 React 函数组件中不使用 useEffect 钩子(Hook)获取数据?

javascript - 通过单击按钮重定向到 jQuery 中的另一个网页

php - PHP 中的重定向/返回检查

即使在删除空格后,PHP header 在 MAMP 中也不起作用