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.

Dispatch more than once in non-batched update cause incorrect result with `useSelector`

See original GitHub issue

Do you want to request a feature or report a bug?

Report a bug.

What is the current behavior?

I’v created a reproducing repo here https://github.com/malash/react-redux-issue-ref/blob/master/reduxBug.js

This repo use both react-redux and react-native.

<Button
  title="[BUG] Click Me (setTimeout)"
  onPress={() => {
    setTimeout(() => {
      dispatch({type: 'NOOP'});
      dispatch({type: 'TOGGLE'});
    }, 0);
  }}
/>

When using setTimeout’s callback to dispatch multi times, the useSelector return incorrect value. The reason why I use setTimeout is to ensure these dispatch calling are non-batched.

const selector = state => ({
	bool: state.bool,
});

const ReduxBugParent = () => {
  const {bool} = useSelector(selector);
  // ...
  <Text>bool from useSelector is {JSON.stringify(bool)}</Text>
  <Text>bool from store.getState is {JSON.stringify(boolFromStore)}</Text>
}

After click the button different selector result are shown. the bool are always different with boolFromStore.

Screen Recording 2019-10-29 at 11 05 55 PM

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to a CodeSandbox (https://codesandbox.io/s/new) or RN Snack (https://snack.expo.io/) example below:

  1. Clone the repo https://github.com/malash/react-redux-issue-ref
  2. Install React Native CLI
  3. Run react-native run-ios
  4. Click buttons and see rendered values.

What is the expected behavior?

This bug can only reproduce on non-web environment, maybe it is related to https://github.com/reduxjs/react-redux/issues/1436

I believe it should works on React Native as well as React DOM.

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.9.0 react-native@0.61.2 react-redux@7.1.1 redux@4.0.4

cc

@byunicorn

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:22 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
bausmeiercommented, Nov 5, 2019

We ran into this issue today and the v7.1.2-alpha.0 release fixes it for us. Perfect timing 💯

1reaction
markeriksoncommented, Nov 5, 2019

Just published v7.1.2-alpha.0 as react-redux@fix-test. Can folks try this out and let me know if it fixes the issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

useSelector not updating when store has changed in Reducer ...
On debug I checked that the state was really changed. But the component is not updating. Component: function Cliente(props) { const dispatch = ......
Read more >
Hooks - React Redux
Because of the React update batching behavior used in React Redux v7, a dispatched action that causes multiple useSelector() s in the same ......
Read more >
Stale props and zombie children in Redux | Kai Hao
For the first solution, due to its bad performance when we only use selector in the render phase causing re-render for every change,...
Read more >
UseSelector and UseDispatch: A Guide to React-Redux Hooks
These hooks allow you to connect to the Redux store and dispatch actions without having to wrap your components in connect(). This guide...
Read more >
Why is my useSelector returning undefined but my state is ...
I've been at this for a while and I can't figure it out. When I dispatch my populateDatasets action I can see that...
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