Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!