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

126
Views
useEffect dependency is not updating the useEffect

I have a useEffect hook that is not updating after changing the dependency. The dependency is a state variable passed through props. Included below is what the code looks like:

const MyComponent = ({resource}) => {

// runs after changing resource state
console.log(resource)

useEffect(() => {
    setLoading(true);
      // doesnt run after changing resource state
      console.log(resource)
      setVariable(setDefaultValue());
    
  }, [resource]);

}

MyComponent is being rendered for one of two states 'option1' or 'option2' the component renders differently depending on the state. I call the component like so:

const [resource, setResource] = useState('option1');
const handleChange = (e) => {
    setResource(e.target.value);
};

return (
    <MyComponent resource={resource} />
)

I don't understand why the useEffect isn't running after resource state is changed. The console.log on the outside of the useEffect show the change state, but the console.log inside of the useffect isn't run after changing state.

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

0

Oh, you can change code the following above try:

//Parent Component
const [resource, setResource] = useState('option1');
const [variable,setVariable] = useState();
const [loading,setLoading] = useState(false);
useEffech(()=>{
  let fetch_api = true;
  setLoading(true);
  fetch(URL,options).then(res=>res.json())
    .then(res=>{
       if(fetch_api){
          setVariable(setDefaultValue());
          setLoading(false);
       }
    });
    return ()=>{
        //remove memory
        fetch_api = false;
    }
},[resource]);
const handleChange = (e) => {
    setResource(e.target.value);
};

return (
    <MyComponent variable={variable} />
)


//Child Component
const MyComponent=({variable})=>{
  
  return <>
    
  </>
}

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!