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

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

About this issue

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

Most upvoted comments

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

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?