【问题标题】:how to redirect to home page after submitting redux-form?提交redux-form后如何重定向到主页?
【发布时间】:2017-06-13 13:21:46
【问题描述】:

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

这是我写的-

form.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>
  )}
  }

【问题讨论】:

  • 你能显示代码吗,关于你是如何调用 Redux 表单的,然后你是如何尝试重定向的
  • @Shubham 我已经更新了问题

标签: reactjs redux-form


【解决方案1】:

为了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);

【讨论】:

  • 非常感谢舒巴姆。你能告诉我如何在 app 中传递这些值以及如何在 consoleof app.js 中打印这些值吗?
  • 我无法在 app.js 访问 props.formStates :(
  • 你能在上一个问题中添加你的应用组件代码吗
  • 这种方法的问题是,如果你有一个失败的请求,无论什么原因,你可能不想重定向......
  • @Will,在这种情况下,当请求成功时,您将有条件地写this.props.history.push('/app');
猜你喜欢
  • 2017-09-27
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 2017-07-25
  • 2016-01-09
  • 1970-01-01
  • 2022-11-03
  • 1970-01-01
相关资源
最近更新 更多