Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
get value from async function in useState

I'm using async storage to retrieve my data but how do I use it with useState()?

Example:

async function getdata(){
let d= await AsyncStorage.getItem('Name');
return d;
}

export default function App() {
 const [name, setName] = useState(() => getdata());
 return (<View>...</View>)
}

but this doesn't work since getdata() is async, so how do I solve this problem?

Edit:

I forgot to mention I tried the useEffect() Hook like this:

async function getdata(){
let d= await AsyncStorage.getItem('Name');
console.log('retrieved Data!');
return d;
}

const [name, setName] = useState(()=>0);
  useEffect(() => {
    getData().then(setName);
    console.log('moving on...');
  }, []);

but the order of execution was:

'Moving on...'
'retrieved Data!'

where as it should have been the other way around

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You'll initialize your state to some empty value. Maybe an empty string, maybe a null. If you like, you can have your component render something different during this time, such as a loading indicator. Then once the data loads, you can set state to render again.

export default function App() {
  const [name, setName] = useState(null);
  useEffect(() => {
    getData().then(value => setName(value));
  }, []);

  if (name === null) {
    return <Text>Loading...</Text>
  } else {
    return (<View>...</View>)
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You have to use useEffect

export default function App() {
  const [name, setName] = useState('');

  useEffect(() => {
    const bootstrap = async () => {
      const name = await getdata();
      setName(name);
    };
    bootstrap();
  }, []);
  return <View>...</View>;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I think the best solution here is to initialize name as false, and then use 'setName' in a .then() callback. setName (and those setter functions that come out of useState generally) is meant to be a way of updating that value asynchronously.

I'm setting name to the boolean 'false' here and checking it strictly. If there is a case where that name field is empty it can just be set to an empty string without causing an infinite loop.

async function getdata(){
let d= await AsyncStorage.getItem('Name');
return d;
}

export default function App() {
 const [name, setName] = useState(false);

 useEffect(() => {
  if (name === false) {
   getData().then((name) => {
    setName(name);
   });
  }
 }, []);
 
 return (<View>...</View>)
}

Keep in mind that your view has to be able to deal with a false name value. How it does that is up to you.

^^^The above solution should effectively solve your problem but I'll make a quick note here: If your component is relatively complex it might be re-rendering a number of times for other reasons and will consequently run the if(name === false) condition as it waits for the callback to resolve. This won't break things but it'll hammer async storage more than necessary and might affect performance. If you find that to be the case, look into 'debouncing' callbacks using the helpful article below. https://medium.com/@gabrielmickey28/using-debounce-with-react-components-f988c28f52c1

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda