Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

edit form with setdata #424

Open
patricpfranca opened this issue Aug 25, 2021 · 2 comments
Open

edit form with setdata #424

patricpfranca opened this issue Aug 25, 2021 · 2 comments

Comments

@patricpfranca
Copy link

Hello,

Description
I'm trying to set data from a api in a form using formRef.current.setData. However, I getting and error because formRef is null. I'm calling a function load inside of useEffect and i'm setting the data at Try

Exception or Error


async function load() {
  const { data } = await api.get('...', {
        headers: {
          Authorization: `Bearer ${token}`,
        },
      });

   formRef.current.setData({
      name: data.name,
      age: data.age,
    });
}

useEffect(() => {
    load();
  }, []);

Screenshots

Captura de Tela 2021-08-25 às 17 47 17

Environment:
"@unform/core": "^2.1.6",
"@unform/mobile": "^2.1.6",
"react": "17.0.1",
"react-native": "0.64.0",

@patricpfranca
Copy link
Author

can anyone help me with this?

@lvsouza
Copy link

lvsouza commented Oct 21, 2021

For the first run, your formRef.current is undefined...

Then you can use a ?...

async function load() {
  const { data } = await api.get('...', {
        headers: {
          Authorization: `Bearer ${token}`,
        },
      });

   formRef.current?.setData({
      name: data.name,
      age: data.age,
    });
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants