简体   繁体   English

redux-form - 仅提交已编辑的字段

[英]redux-form - Submit only edited field

In this example, I have a form to update user information and it is written in React with redux-form 6.5. 在这个例子中,我有一个用于更新用户信息的表单,它用React用redux-form 6.5编写。 I'm a newbie with this stack. 我是这个堆栈的新手。

The render form function is like: 渲染表单功能如下:

render() {
    const { handleSubmit } = this.props;        
    return (
      <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
        <Field name="name" component="input" type="text"/>
        <Field name="surname" component="input" type="text"/>
        <button action="submit">Sign in</button>
      </form>
    );

I have the reduxForm connection: 我有reduxForm连接:

const extendedComponent = reduxForm({
  form: 'userdetail',
  validate
})(UserDetail);

And I have submit handler: 我有提交处理程序:

 handleFormSubmit(user) {
    // TODO: how can I get only the touched fields?
    this.props.updateUser(user);
  }

I receive correctly the object user after the validation, I send the PUT call and everything works fine. 我在验证后正确接收了对象用户,我发送了PUT调用,一切正常。

But I don't like to send all the data in the PUT, my wish is to send only the edited data to the PUT . 但是我不喜欢在PUT中发送所有数据, 我希望只将编辑后的数据发送到PUT I understand that I could retrieve the initialValues and compare all fields. 我知道我可以检索initialValues并比较所有字段。

There is another way? 还有另外一种方法吗? How can I get only the touched fields? 我怎样才能获得触摸的字段?

Which is the best practice to do this? 这是最好的做法吗?

It seems to me a common task and I'm not finding anything about that, so I am assuming I'm completely missing the point. 在我看来,这是一个共同的任务,我没有找到任何相关的东西,所以我假设我完全忽略了这一点。

Thank you all :) 谢谢你们 :)

According to the maintainer of the redux-form project: "That's not a feature of the library" . redux-form项目的维护者说: “这不是图书馆的一个特色”

In that response, he recommends handling the diffing yourself or using a pre-existing library, like object-diff . 在该响应中,他建议您自己处理差异或使用预先存在的库,例如object-diff

For example like this: 例如这样:

import diff from 'object-diff'

handleFormSubmit(user, dispatch, props) {
  const { initialValues } = props 
  const changedValues = diff(initialValues, user)
  this.props.updateUser(changedValues);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM