Skip to content

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

Closed
@malash

Description

@malash

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 #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions