相关文章推荐
帅呆的佛珠  ·  Let Me Game in Peace ...·  3 年前    · 

RFC: async defaultValues #2492

@bluebill1049

Description

Summary

The current status of setting defaultValues from async data source is using reset :

useEffect




    
(() => {
  reset({
     firstName: 'test',
});

honestly, this is probably not a pretty and clean approach... a few weeks before we had a feature request regarding async defaultValues : #2335

Proposal Solutions

  • defaultValues accept Promise , and promise will get resolve after useEffect , although I do have concern over the confusion which prop support both object and Promise .
  • const { formState: { isLoading } } = useForm({
      defaultValues: async () => ({ firstName: 'test' })
      // defaultValues: new Promise()
    }, [])

    happy to discuss more options on the table too.