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.

selector in useSelector called one more time after forceUpdate inside hook

See original GitHub issue

What is the current behavior?

When state changed, selector recomputes, and value sets into latestSelectedState. Component will re-render, and selector recomputes again, but its value already in latestSelectedState.current

(selector is arrow function - const data = useSelector((state) => state.data);)

What is the expected behavior?

Selector recomputes once

I can open PR to fix this, but I don’t know, may be selector specially calls twice for some reasons

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Apr 17, 2021

@phantomcosmonaut no, that shouldn’t have anything to do with this issue. This is about a selector re-running when the component is already rendering. What you’re describing is a store subscription causing a component to render in the first place.

If you’re seeing problems with React-Redux and Suspense, please file a new issue with a reproduction so we can look into it.

0reactions
phantomcosmonautcommented, Apr 17, 2021

Hey, I was using Suspense and a custom hook to render components asynchronously with async data fetching redux actions and I seem to have run into an issue directly related to this thread. I get the error Cannot update a component ('IdentityVerificationScreen') while rendering a different component ('RenderSuspender'). To locate the bad setState() call inside 'RenderSuspender' which takes me to the forceRender call from useSelector hook. Do you think the update in 7.2.4 will solve this issue and when can I expect to see the new version released? Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

useSelector() not re-rendering component on update
A useEffect hook with dependency array [] will only run one time when the component is first mounted. Your fetch actions depend on...
Read more >
How useSelector can trigger an update only when we want ...
The useSelector hook from react-redux doesn't have this issue — components only re-render when their selected piece of state changes, even when ...
Read more >
Hooks FAQ
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. This page...
Read more >
Hooks
You may call useSelector() multiple times within a single function component. Each call to useSelector() creates an individual subscription to ...
Read more >
useSelector with equality check still triggers useEffect #1381
useSelector is a function that returns one value, so we do a reference equality comparison to see if that one value has changed...
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