question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] 'asyncSubmit' prop for the Field component

See original GitHub issue

This was originally proposed by @peteruithoven as a comment here and here, but at the time, @KaboomFox’s solution (thank you for that!) was more immediate and solved the immediate problem mentioned in #980.

Having used that solution in my code, it feels more like a temporary solution but not ‘the’ solution. Through that PR, we get the fieldName that triggered the asyncValidate function. That works well where all the form input is always being submitted together to validate or submit (even in this case, there is boilerplating around not duplicating errors).

However, lets take a simple case, a form with many fields, where each value can or needs to be validated or saved separately onBlur (for performance and UX reasons) to the backend for validation or saving. In that case, the current solution can work but with a lot more boilerplate around checking state of requests, for spinners, or not duplicating requests etc.

In addition, by having it as a prop to the Field component, I am hoping we can also leverage the meta or other props to the field in this function. Currently, all that state needs to be held in parent or decorated component and doesn’t feel like a clean solution.

Alternatively, auto-saving forms can use onBlur prop on fields or be a collection of little forms.

Thoughts? @erikras @KaboomFox @peteruithoven

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
erikrascommented, Nov 27, 2016

Hmm…

Many Little Forms

Depending on how cohesive the form data is, how much it needs to be treated as a group of values, it would be pretty easy to make a little form for each field. A settings screen full of toggles is a great example of this. But for a single checkbox, it’s questionable if it’s worth using a whole form library just to keep track of two literal bits (checked and submitting) of information.

Hijacking asyncValidate

You could also (mis-)use asyncValidate to send the values to the server and save them, but this will break the meaning of dirty/pristine.

Submit on onBlur

Your field renderer could submit the whole form whenever any of your fields lose focus, but this has consequences if the other fields you haven’t touched have submit errors, it’s going to mark them all as touched and display the errors.

Proposal

I’m just spitballing here… What if Field had a submitOnBlur boolean prop that, when set, would submit the form (all the values), but would not mark all the other fields as touched. The Field component would be given a meta.submittingField or meta.blurSubmitting flag (to display a spinner) that would be true for the life of the submit promise. Any errors coming back from the server would only set the submitError for that field.

Does that sound more like “the” solution?

1reaction
oyeanujcommented, Dec 6, 2017

@gustavohenke While I understand that this issue has been inactive, it has been waiting for a response from @erikras and other maintainers.

This is still an unresolved issue, IMO, and should be reopened. I think we should propose the solution if building auto-saving forms. Ideally, one of the ones that @erikras recommended above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reduxForm(config:Object)
a function that takes all the form values, the dispatch function, the props given to your component and the current blurred field, and...
Read more >
React Suspense: Async rendering in React - LogRocket Blog
With Suspense, you have the ability to suspend component rendering while async data is being loaded. You can pause any state update until...
Read more >
Update on Async Rendering – React Blog
It can return an object to update state , or null to indicate that the new props do not require any state updates....
Read more >
Redux Fundamentals, Part 6: Async Logic and Data Fetching
Inside that function, we were able to write some async logic (an HTTP request), then dispatch a normal action object when the request...
Read more >
Component API Reference - Pipedream
Props are custom attributes you can register on a component. When a value is passed to a prop attribute, it becomes a property...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found