如何在Reactjs的新react-router-dom中使用Redirect [英] How to use Redirect in the new react-router-dom of Reactjs
问题描述
我正在使用最新版本的react-router模块,名为react-router-dom,在使用React开发Web应用程序时已成为默认模块。我想知道如何在POST请求后进行重定向。我一直在制作这段代码,但在请求之后,没有任何反应。我在网上查看,但所有数据都是关于反应路由器的先前版本,而上次更新没有。
I am using the last version react-router module, named react-router-dom, that has become the default when developing web applications with React. I want to know how to make a redirection after a POST request. I have been making this code, but after the request, nothing happens. I review on the web, but all the data is about previous versions of the react router, and no with the last update.
代码:
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;
推荐答案
你必须使用 setState
设置一个属性,该属性将在 render()
中呈现< Redirect>
方法。
You have to use setState
to set a property that will render the <Redirect>
inside your render()
method.
例如
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
You can also see an example in the official documentation: https://reacttraining.com/react-router/web/example/auth-workflow
那就是说,我建议你把API调用放在服务中。然后你可以使用 history
对象以编程方式进行路由。这就是与redux 有效。
That said, I would suggest you to put the API call inside a service or something. Then you could just use the history
object to route programatically. This is how the integration with redux works.
但我想你有理由这样做。
But I guess you have your reasons to do it this way.
这篇关于如何在Reactjs的新react-router-dom中使用Redirect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!