最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - redux-form - Submit only edited field - Stack Overflow

matteradmin7PV0评论

In this example, I have a form to update user information and it is written in React with 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" ponent="input" type="text"/>
        <Field name="surname" ponent="input" type="text"/>
        <button action="submit">Sign in</button>
      </form>
    );

I have the reduxForm connection:

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.

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. I understand that I could retrieve the initialValues and pare all fields.

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 mon task and I'm not finding anything about that, so I am assuming I'm pletely missing the point.

Thank you all :)

In this example, I have a form to update user information and it is written in React with 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" ponent="input" type="text"/>
        <Field name="surname" ponent="input" type="text"/>
        <button action="submit">Sign in</button>
      </form>
    );

I have the reduxForm connection:

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.

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. I understand that I could retrieve the initialValues and pare all fields.

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 mon task and I'm not finding anything about that, so I am assuming I'm pletely missing the point.

Thank you all :)

Share Improve this question asked Feb 9, 2017 at 21:48 LattanzioLattanzio 1031 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

According to the maintainer of the redux-form project: "That's not a feature of the library".

In that response, he remends handling the diffing yourself or using a pre-existing library, like 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);
}
Post a comment

comment list (0)

  1. No comments so far