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

98
Views
Accessing Redux selector value in a custom hook

I'm writing my first hook and I also use redux. I'm listening to orientation sensor data from the mobile device in React native and when it updates I filter it to see if the new value is different than the old value before I send it over to redux state to be stored.

I'm noticing in the filterData method the data is always null. But if I console log the storedVal in the render I see it's always being updated and it's updated in the Redux store as well.

But for some reason filterData only has access to the initial value and doesn't get the updated values. Any idea how to fix this?

  const storedVal = useSelector(selectors.selectStoredVal);

  const filterData = useCallback(
    () => {
      console.log(storedVal);
    },
    [storedVal]
  );

  useEffect(
     () => {
        orientation.pipe(filter(filterData)).subscribe((data) => {
             console.log('Data filtered correctly, new data received here');
        }
     }
  ), 
  [];
}

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

0

An empty dependency array for useEffect means that the callback function within it will only run once (on component mount).

useCallback is creating a function that is being passed into the filter function. Because storedVal is a dependency for filteredData, went storedVal changes and filteredData is updated to a new function, your useEffect still has a reference to it's old value.

Try adding [filteredData] to your dependency array for the useEffect.

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!