Monday, 4 October 2021

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?



from SET method is not working the first time unless app is refreshed - React Native

No comments:

Post a Comment