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

107
Views
Custom hook not updating in component

I have a custom hook that looks roughly like this:

export default function useMyHook() {
  const [foo, _setFoo] = useState([]);
  const [bar, setBar] = useState(null);
  const setFoo = (arg) => {
    _setFoo(arg);
    const newBar = getBar(arg);
    setBar(newBar);
  };

  useEffect(() => {
    console.log(bar); // this prints when bar changes
  }, [bar]);

  return {
    foo,
    bar,
    setFoo,
  };
}

And this hook is used like this:

export const function MyComp(): React.MixedElement {
  const {foo} = useMyHook()

  console.log(foo) // prints null once
  useEffect(() => {
    console.log(foo) // doesn't print
  }, [foo])

  return foo ? (
    <div>This doesn't render</div>
  ) : (
    <div>This renders</div>
  )
} 

The setFoo() is called in different component to set foo. When that happens, bar changes, and runs the useEffect callback, but not in the component.

What is happening here, and how can I fix it?

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!