question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useSelector returning out-of-date result

See original GitHub issue

What is the current behavior?

See this sandbox.

After deleting an entry, useSelector in the ListHook component returns an out-of-date result including the ID of the deleted entry. This leads to an Item calling useSelector for the missing item (this is logged to the console). It looks to me like it might be because the selector is run inside an effect hook.

The ListClass component uses the old connect approach and doesn’t have the same issue.

What is the expected behavior?

The behaviour of the hook has caused some issues while migrating an app where selectors currently throw exceptions. I understand the hook isn’t meant to be a drop-in replacement for the old approach but I’m interested to see if there is a way to avoid this while still using hooks.

Which versions of React, ReactDOM/React Native, Redux, and React Redux are you using? Which browser and OS are affected by this issue? Did this work in previous versions of React Redux?

react 16.8.6 react-dom 16.8.6 react-redux 7.1.0 redux 4.0.1

Issue exists in all 7.1.0 alphas and RC.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
hednowleycommented, Jun 13, 2019

I will check the package out, thank you.

FYI I noticed that in my sandbox example the staleness exceptions are caught by react-redux (by design) but that they aren’t in my real-world application. I was able to reproduce my real-world issue by adding a wrapping component to a new sandbox and noticed something interesting: the staleness error is thrown by the selector on 7.1.0 but not on 7.1.0-alpha4. It looks like this is caused by this optimisation change.

Just mentioning in case this is an opportunity to mitigate some “zombie child” issues.

1reaction
markeriksoncommented, Jun 12, 2019

Yep.

Also, as an unrelated side note, you really shouldn’t be putting mutable ES6 Map instances into the state.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is useSelector is called with outdated state - React Redux
Every time that sequence is changed - you create new store subscribe on useEffect hook - more and more. You can make it...
Read more >
How useSelector can trigger an update only when we want it to
While my useSelector correctly returns the state of the store, ... and then if that update results in a change to the selected...
Read more >
Be Aware of Stale Closures when Using React Hooks
The stale closures is a pitfall of React hooks when an outdated variable is captured by a closure.
Read more >
React hooks (v17.0.3) and Redux handbook using TypeScript ...
map() will always return a new array reference. The useSelector() hook will be run after every action is dispatched. And since this will...
Read more >
Creating React Apps With Redux Toolkit and RTK Query - Toptal
Such imports usually result in an increased bundle size, but with the ... useDispatch, useSelector } from 'react-redux'; import { Reducer } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found