When asynchronously updating the state of a reusable store multiple times, sometimes the final update is not reflected in the state that is returned to the subscriber.
This appears to only happen when updating the state an even number of times, from within an async function. The value remains stale until the state is updated again.
The reproduction below uses a contrived example with Promise.resolve, but the issue seems to occur for any async function (i.e. after fetching data, setTimeout callback, etc).
Reproduce
Sandbox reproduction:
https://codesandbox.io/s/reusablejs-react-17-bug-4hg8n?file=/src/App.js
This same code seems to be working on React 16:
https://codesandbox.io/s/reusablejs-react-16-no-bug-g8jz5?file=/src/App.js
Additional Info
- package.json (from codesandbox above):
{
"name": "reusablejs-react-17-bug",
"version": "1.0.0",
"description": "",
"keywords": [],
"main": "src/index.js",
"dependencies": {
"react": "17.0.2",
"react-dom": "17.0.2",
"react-scripts": "4.0.0",
"reusable": "1.0.1"
},
"devDependencies": {
"@babel/runtime": "7.13.8",
"typescript": "4.1.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
- which browser
Chrome, Safari
When asynchronously updating the state of a reusable store multiple times, sometimes the final update is not reflected in the state that is returned to the subscriber.
This appears to only happen when updating the state an even number of times, from within an async function. The value remains stale until the state is updated again.
The reproduction below uses a contrived example with
Promise.resolve, but the issue seems to occur for any async function (i.e. after fetching data,setTimeoutcallback, etc).Reproduce
Sandbox reproduction:
https://codesandbox.io/s/reusablejs-react-17-bug-4hg8n?file=/src/App.js
This same code seems to be working on React 16:
https://codesandbox.io/s/reusablejs-react-16-no-bug-g8jz5?file=/src/App.js
Additional Info
{ "name": "reusablejs-react-17-bug", "version": "1.0.0", "description": "", "keywords": [], "main": "src/index.js", "dependencies": { "react": "17.0.2", "react-dom": "17.0.2", "react-scripts": "4.0.0", "reusable": "1.0.1" }, "devDependencies": { "@babel/runtime": "7.13.8", "typescript": "4.1.3" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject" }, "browserslist": [ ">0.2%", "not dead", "not ie <= 11", "not op_mini all" ] }Chrome, Safari