当前位置: 首页 > 知识库问答 >
问题:

如何在Reactjs新的react-router-dom中使用重定向

慕烨烁
2023-03-14

我使用的是上一个版本的react-router模块,名为react-router-dom,它已经成为用react开发web应用程序时的默认设置。我想知道如何在POST请求后进行重定向。我一直在做这个代码,但在请求之后,什么也没有发生。我在网上查看,但所有的数据都是关于react路由器的早期版本,没有最近的更新。

代码:

import React, { PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { Redirect } from 'react-router'

import SignUpForm from '../../register/components/SignUpForm';
import styles from './PagesStyles.css';
import axios from 'axios';
import Footer from '../../shared/components/Footer';

class SignUpPage extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      errors: {},
      client: {
        userclient: '',
        clientname: '',
        clientbusinessname: '',
        password: '',
        confirmPassword: ''
      }
    };

    this.processForm = this.processForm.bind(this);
    this.changeClient = this.changeClient.bind(this);
  }

  changeClient(event) {
    const field = event.target.name;
    const client = this.state.client;
    client[field] = event.target.value;

    this.setState({
      client
    });
  }

  async processForm(event) {
    event.preventDefault();

    const userclient = this.state.client.userclient;
    const clientname = this.state.client.clientname;
    const clientbusinessname = this.state.client.clientbusinessname;
    const password = this.state.client.password;
    const confirmPassword = this.state.client.confirmPassword;
    const formData = { userclient, clientname, clientbusinessname, password, confirmPassword };

    axios.post('/signup', formData, { headers: {'Accept': 'application/json'} })
      .then((response) => {
        this.setState({
          errors: {}
        });

        <Redirect to="/"/> // Here, nothings happens
      }).catch((error) => {
        const errors = error.response.data.errors ? error.response.data.errors : {};
        errors.summary = error.response.data.message;

        this.setState({
          errors
        });
      });
  }

  render() {
    return (
      <div className={styles.section}>
        <div className={styles.container}>
          <img src={require('./images/lisa_principal_bg.png')} className={styles.fullImageBackground} />
          <SignUpForm 
            onSubmit={this.processForm}
            onChange={this.changeClient}
            errors={this.state.errors}
            client={this.state.client}
          />
          <Footer />
        </div>
      </div>
    );
  }
}

export default SignUpPage;

共有1个答案

夏侯枫
2023-03-14

必须使用setstate来设置一个属性,该属性将在render()方法中呈现

例如。

class MyComponent extends React.Component {
  state = {
    redirect: false
  }

  handleSubmit () {
    axios.post(/**/)
      .then(() => this.setState({ redirect: true }));
  }

  render () {
    const { redirect } = this.state;

     if (redirect) {
       return <Redirect to='/somewhere'/>;
     }

     return <RenderYourForm/>;
}

您还可以在官方文档中看到一个示例:https://reacttraining.com/react-router/web/example/auth-workflow

 类似资料:
  • 问题内容: 我正在使用最新版本的react-router模块,名为react-router- dom,它已成为使用React开发Web应用程序时的默认模块。我想知道在POST请求后如何进行重定向。我一直在编写此代码,但是在请求之后,什么都没有发生。我在网上查看过,但是所有数据都是关于React Router的先前版本的,而关于最后一个更新的则不是。 码: 问题答案: 您必须使用设置一个属性,该属性

  • 我完全卡住时集成HOC在我的react.js项目。 这是我的路线文件 我对此完全崩溃了,但没有摆脱那个问题。为什么我的控制台中的不显示值 有任何问题与反应和反应路由器的版本 提前谢谢你!!!

  • 我正在尝试构建一个简单的组件,它将进行一次API调用,2秒钟后将重定向到另一个页面。为此,我尝试使用react router dom中的重定向组件,但导入失败 我得到一个错误:“react router dom没有导出的成员‘重定向’。” 版本"react-router-dom":"^6.0.0-beta.0",

  • 两者都有路由、链接等。什么时候使用其中一种?我真的很困惑在哪里使用每一个。服务器端?客户端? https://reacttraining.com/react-router/ 在一些例子中,你需要通过历史,在另一些例子中则不需要。要怎么做? vs 什么时候使用一个或另一个真的很混乱,任何帮助都值得赞赏。

  • 问题内容: 我非常了解我想对组件进行条件渲染的react-router-dom。如果未登录use,则将他重定向到某个第三方URL 例如,下面的代码看起来很整洁,可以正常工作 让我们在上面的示例中说,如果我想重定向 https://www.google.com, 该怎么做? 如果我写 如何重定向到第三方网站? 问题答案: 您可以将标记用于外部网址, 但您也可以提供这样的组件:

  • 我有类似这样的App.js文件 而看起来是这样的 我需要使,和工作 我尝试了上述方法,但输出是空白的,当我去任何这些路线。只有Navbar被渲染。