Unform: Add async function support in initialData prop

Created on 28 May 2019  路  5Comments  路  Source: unform/unform

What would you like to be added:

When I use other libraries like Formik, I have some trouble about async data, double loading (page loading using dynamic imports and data loading) is not a good approach for UX. Why not use a Promise resolve to get initialData?

Why is this needed:

I think about a simple function inside initialData prop like this:

async function fetchProfileInformation(){
    const res =  await fetch("/somelink");
    return res.data;
}

<Form initialData={fetchProfileInformation} >
    {...}
</Form>

Additional context

In typescript, a simple enhancement in prop verification is:

initialData: {} | () => Promise<{}>

Thank's for the attention.

feature request

Most helpful comment

I think @diego3g is saying you could do something like this:

export function Component() {
  const [initialData, setInitialData] = useState(null);

  async function getInitialData() {
    const res = await api.get("/somelink");
    setInitialData(res.data);
  }

  // display loading here...
  // example: {!initialData && <Loading />}

  return (
    {initialData && (
      <Form initialData={initialData}>
        {/** ... */}
      </Form>
    )}
  )
}

All 5 comments

@SorEduard It would not be a use case with state, as the example below:

export function Component() {
  const [initialData, setInitialData] = useState({});

  async function getInitialData() {
    const res = await api.get("/somelink");
    setInitialData(res.data);
  }

  return (
    <Form initialData={initialData}>
      {/** ... */}
    </Form>
  )
}

@italoiz Yes this approach work's, but cause flash in UI, why not solve data before render the form? I do not know if this is really necessary because there are innumerable methods to get data before route resolve, but in some libraries like apollo-graphql, this give a lot of difficulty to make, using prefetch before history.push, in SSR to. Thanks for reply.

@SorEduard I understand your thoughts but i don't think it's unform responsability wait for data to load, as the user may use many different types of loading like shimmer placeholders (like this) or a loading indicator.

Maybe you should wrap the form with a conditional to render it only when you finish loading the data.

What do you think?

I think @diego3g is saying you could do something like this:

export function Component() {
  const [initialData, setInitialData] = useState(null);

  async function getInitialData() {
    const res = await api.get("/somelink");
    setInitialData(res.data);
  }

  // display loading here...
  // example: {!initialData && <Loading />}

  return (
    {initialData && (
      <Form initialData={initialData}>
        {/** ... */}
      </Form>
    )}
  )
}

@diego3g makes sense, was just a suggestion, the different approaches suggested would solve the issue well, thank you, closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

filipecoelho picture filipecoelho  路  5Comments

lucascco picture lucascco  路  5Comments

korczas picture korczas  路  5Comments

medeiroshudson picture medeiroshudson  路  3Comments

gabrielbuzziv picture gabrielbuzziv  路  5Comments