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

168
Visualizações
SET method is not working the first time unless app is refreshed - React Native

I am trying to setPackages in useEffect everything is working fine except setPackages method. It do not work first time but when I refresh app by saving CTRL+S from VS Code it works and setPackages works.

Following is the code.

const [packages, setPackages] = useState([]);

useEffect(() => {
   const getPackages = async () => {
      Purchases.setDebugLogsEnabled(true);
      Purchases.setup("MY_KEY_HERE");
      try {
        const offerings = await Purchases.getOfferings();
        if (offerings.current !== null) {    
           console.log(offerings.current.availablePackages); //This displays the data (works)
           setPackages(offerings.current.availablePackages); //This do not set the data in packages unless app refreshed
        }else{
            console.log("No offerings found");
        }
      } catch (e) {
         console.log("Error => " + e);
      }
   }
   getPackages();
},[])

I need to refresh app every time to make this line work. What could be issue here?

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

0

You are declaring the function, but not calling it. And you should include an empty deps array in useEffect if you want this to be call once on start and not over and over again.

const [packages, setPackages] = useState([]);

useEffect(() => {
   const getPackages = async () => {
      Purchases.setDebugLogsEnabled(true);
      Purchases.setup("MY_KEY_HERE");
      try {
        const offerings = await Purchases.getOfferings();
        if (offerings.current !== null) {    
           console.log(offerings.current.availablePackages); //This displays the data (works)
           setPackages(offerings.current.availablePackages); //This do not set the data in packages unless app refreshed
        }else{
            console.log("No offerings found");
        }
      } catch (e) {
         console.log("Error => " + e);
      }
   };
   getPackages();
},[])
about 4 years ago · Juan Pablo Isaza Relatório

0

May I suggest this hook to handle your packages-state?

Use it like this:

function MyComponent() {
  // Load packages (will be undefined while loading or updating)
  const packages = usePromiseMemo(() => {
    Purchases.setDebugLogsEnabled(true);
    Purchases.setup('MY_KEY_HERE');
    return Purchases.getOfferings().then(offerings => {
      if (offerings.current) {
        console.log(offerings.current.availablePackages); //This displays the data (works)
        return offerings.current.availablePackages;
      } else {
        console.log('No offerings found');
      }
      return undefined;
    });
  }, []); // Alternatively: Replace [] with [Purchases]

  // Render
  return <div>More stuff here</div>;
}

I don't know where Purchases comes from, but add it to the dependency array if it is subject to change.

Also, if 'MY_KEY_HERE' is stored in some variable, add that to the dependency array too.

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