gpt4 book ai didi

reactjs - 如何在提交 redux-form 后重定向到主页?

转载 作者:行者123 更新时间:2023-12-04 16:09:50 25 4
gpt4 key购买 nike

我创建了一个 redux 表单,当点击提交按钮时,它不会重定向到 app.js 页面,url 更改为此 http://localhost:3000/?name=Cook&phone=45625465265&email=cook %40yahoo&work=Engineer&city=

这是我写的-

表单.js

const Form=({handleSubmit})=>(
<form onSubmit={handleSubmit}>
<center>
<div>
<label>First Name</label>
<Field type="text" component="input" placeholder="Name" name="name"/>
</div>
<div>
<label>Address</label>
<Field type="text" component="input" placeholder="Phone" name="phone" />
</div>
<button type="submit">Submit</button>
</center>
</form>
)


export default reduxForm({
form: 'form',
fields: ['name', 'address']
})(Form);

添加.js

class AddNew extends Component{
handleSubmit = (values) => {
console.log('This Values',values)
}
render() {


return (
<div style={{ padding: 150 }}>
<Add onSubmit={this.handleSubmit}/>
</div>
)}
}

最佳答案

为了Redirect,你可以做的是在AddNew组件的handleSubmit函数中,你可以调用动态路由this.props.history.push()

现在假设你的 App.js 是在路径 /app 的路由中指定的

您可以执行以下操作

import {withRouter} from 'react-router'

class AddNew extends Component{

handleSubmit = (values) => {
console.log('This Values',values)
this.props.history.push('/app');
}
render() {


return (
<div style={{ padding: 150 }}>
<Add onSubmit={this.handleSubmit}/>
</div>
)}
}

export default withRouter(AddNew);

关于reactjs - 如何在提交 redux-form 后重定向到主页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44522811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com