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

73
Views
react hook useEffect cleanup with dynamic prop problem

In our application we are attempting to leverage useEffect to make an api call on cleanup of a component, something like this:

useEffect(() => {
  return () => {
    myAPIFunction(dynamicProp)
    // api call here that leverages a prop
  }
})

The idea here is that we want to make this api call on the tear down of this component. The problem is that as our application progresses dynamicProp changes at the component level, but whenever the api call is made we get the value dynamicProp as whatever it was initialized at.

If we update it it to the following:

useEffect(() => {
  return () => {
    myAPIFunction(dynamicProp)
    // api call here that leverages a prop
  }
}, [dynamicProp])

It does update the prop in the cleanup function but we really only want to call this once on the component cleanup, and this causes the cleanup to fire any time dynamicProp changes.

Is there a solution where we can have dynamicProp be .... dynamic in our cleanup without having to run the cleanup every time dynamicProp changes?

about 4 years ago · Juan Pablo Isaza
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!