Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ‘―β€β™‚οΈ support async defaultValues and form values update #9261

Merged
merged 67 commits into from Dec 4, 2022

Conversation

bluebill1049
Copy link
Member

@bluebill1049 bluebill1049 commented Oct 24, 2022

Context

This PR is aimed to improve both UX and DX with form values update.

  • To improve DX by removing redundant useEffect and reset combination, eg:
useEffect(() => {
  reset(defaultValues)
}, [reset, defaultValues])
  • To promote better UX with server state updates.

Proposal Syntax

πŸ‘‰ NEW values props

The following syntax will react to values prop update/changes.

  • values will be reactive to update/change and reset accordingly
  • provide a reset option to keep dirty/touched values potentially
const values = await fetch('API')

useForm({
  values, // will reset the form when values updates
  // resetOptions: {
  //   keepDirtyValues: true
  // }
})

πŸ‘‰ NEW async defaultValues props

The following syntax will support async defaultValues, so we will manage the reset form internally and update formState accordingly.

  • promise will only be resolved once during useForm() call
  • It's possible to supply resetOptions as well
useForm({
  defaultValues: fetch('API')
  // resetOptions: {
  //   keepDirtyValues: true
  // }
})

React use API

useForm({
  defaultValues: use(fetch('API'))
  // resetOptions: {
  //   keepDirtyValues: true
  // }
})

RFC

πŸ™‹ What's the difference between values and defaultValues

values will react to changes and reflect on the form values, and defaultValues is cached for once and will never re-run the promise or react to defaultValues changes.

TODO:

  • type inference for async defaultValues
  • documentation update
  • testing in real app

@codesandbox
Copy link

codesandbox bot commented Oct 24, 2022

CodeSandbox logoCodeSandbox logoΒ  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@bluebill1049 bluebill1049 changed the title πŸ›Ί auto reset form values with reactive values prop πŸ§ͺ POC: support async defaultValue Oct 24, 2022
@bluebill1049 bluebill1049 changed the title πŸ§ͺ POC: support async defaultValue πŸ§ͺ POC: support async form values update Oct 24, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Oct 24, 2022

Size Change: +148 B (+1%)

Total Size: 18.7 kB

Filename Size Change
dist/index.cjs.js 9.28 kB +72 B (+1%)
dist/index.umd.js 9.38 kB +76 B (+1%)

compressed-size-action

@bluebill1049 bluebill1049 changed the title πŸ§ͺ POC: support async form values update πŸ§ͺ support async defaultValues and form values update Oct 27, 2022
@bluebill1049 bluebill1049 changed the title πŸ§ͺ support async defaultValues and form values update πŸ‘―β€β™‚οΈ support async defaultValues and form values update Oct 27, 2022
@juliaschiller150
Copy link

juliaschiller150 commented Nov 21, 2022

@bluebill1049 Thanks so much for this! We have a great use case for this.

I'm running the canary and having trouble getting this working with Apollo's useQuery. We need to massage the incoming data first, too, before passing it along to useForm, and I'm not sure how to do that with this interface which runs the defaultValues function once.

Can we explore Apollo use cases as part of this effort?

@bluebill1049
Copy link
Member Author

bluebill1049 commented Nov 21, 2022

@bluebill1049 Thanks so much for this! We have a great use case for this.

I'm running the canary and having trouble getting this working with Apollo's useQuery. We need to massage the incoming data first, too, before passing it along to useForm, and I'm not sure how to do that with this interface which runs the defaultValues function once.

Can we explore Apollo use cases as part of this effort?

hey @tcschiller you can use values props

https://twitter.com/HookForm/status/1593066930579333120

Going to release another patch to improve type inference and improve resetOptions

resetOptions: https://twitter.com/HookForm/status/1593130527292542976

@bluebill1049 bluebill1049 merged commit fcd7985 into master Dec 4, 2022
@bluebill1049 bluebill1049 deleted the useForm-values-prop branch December 4, 2022 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants