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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃И POC: support async defaultValues #9052

Closed
wants to merge 3 commits into from

Conversation

bluebill1049
Copy link
Member

@bluebill1049 bluebill1049 commented Sep 17, 2022

RFC: #9046

Proposal Syntax

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

const fetchFormValues = () => {
  const values = fetch('API')

  return {
    values,
    resetOptions: {
       keepDirtyValues: true,
    }
  }
}

const { formState: { isLoading } } = useForm({
  defaultValues: fetchFormValues
})

// you can display a loading content or disable the form for better UX
if (isLoading) {
  return <div>Loading...</div>;
}

return <form />

@codesandbox
Copy link

codesandbox bot commented Sep 17, 2022

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

@bluebill1049 bluebill1049 changed the title support async defaultValues POC: support async defaultValues Sep 17, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Sep 17, 2022

Size Change: +89 B (0%)

Total Size: 18.1 kB

Filename Size Change
dist/index.cjs.js 9.02 kB +49 B (+1%)
dist/index.umd.js 9.12 kB +40 B (0%)

compressed-size-action

@bluebill1049 bluebill1049 changed the title POC: support async defaultValues 馃И POC: support async defaultValues Sep 17, 2022
@lemcoder
Copy link

lemcoder commented Oct 9, 2022

Will isDirty work correctly with this approach?
In my understanding if I pass async function as defaultValues the only thing it will do is to prefill form inputs. The actual _defaultValues object will stay empty, so isDirty will be always true.

@arthurmmedeiros
Copy link

This will be really useful, is it going to be available soon by any chance?

@bluebill1049
Copy link
Member Author

ng if I pass async function as defaultValues the only thing it will do is to prefill form inputs. The actual _defaultValues object will stay empty, so isDirty will be always true.

should work, will attach unit test coverage for use case as such.

@bluebill1049
Copy link
Member Author

This will be really useful, is it going to be available soon by any chance?

on the project board, will get to it eventually. no ETA at the moment.

@oste
Copy link

oste commented Oct 15, 2022

I am wondering how this would work when using something more declarative like react-query or swr? In those cases the data would be provided more like this where data would be undefined until the async operation completes.

const { data } = useData();

useForm({
defaultValues: data?.title
});

@bluebill1049
Copy link
Member Author

I am wondering how this would work when using something more declarative like react-query or swr? In those cases the data would be provided more like this where data would be undefined until the async operation completes.

const { data } = useData();

useForm({ defaultValues: data?.title });

won't work, will need to use reset at useEffect

@bluebill1049 bluebill1049 reopened this Oct 24, 2022
@bluebill1049 bluebill1049 deleted the feature/aysnc-defaultValues branch October 24, 2022 04:18
@bluebill1049
Copy link
Member Author

#9261

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

4 participants