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

127
Views
data querying issue when using subscribe to get real-time updates in my app

I'm coding a react native mobile application with aws amplify as my backend,

To get real-time updates in my app I used subscribe like this inside a useEffect hook,

const[cartProducts, setcartProducts] = useState <CartProduct[]>([]);
const getCartProduct = async () => {
  await DataStore.query(CartProduct, c=>c.userID("eq", currentUserId))
                 .then(setcartProducts)
};
            
useEffect(() => {       
  const subscription = DataStore.observe(CartProduct, d=>d.userID("eq", UserId))
                                .subscribe(msg => getCartProduct(),);
  return subscription.unsubscribe;
}, []);

the querying of data happens inside getCartProduct() function,

But the problem is whenever I close my app and reopen this screen the fetching function(getCartProduct) doesn't run I guess because there is no change in the data (cartProducts) in the backend.

When ever I change the cartProducts data in backend then the fetching function (getCartProduct) run again

what is the solution to this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Call getCartProduct in the useEffect block before subscribing
  2. Wrap the getCartProduct() callback in curly brackets
  3. Wrap subscription.unsubscribe in curly brackets and call it with ()
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!