Comments (6)
It's not supported to setTimeout
during rendering. Rendering is supposed to be pure and not have side effects. So from that point on, it doesn't make sense to try to reason about what the code is doing.
from react.
To clarify, since this might come across as overly defensive. I think there is sometimes an assumption that once you break the rules, it's okay to think about what the code does, because it should still behave in some meaningful way. So I wanted to convey my perspective that I work on the React team, and I usually give up as soon as I see some rule being broken. It's like there was a random variable in the middle of your calculation. At this point all my assumptions about how React works go out of the window because they don't matter.
Yes, I can try to retrace it, and explain what exactly goes wrong here. It would take some amount of effort. I can try to to do this. But ultimately a bug report needs to contain code that does not break the rules. Code that breaks the rules is never expected to work in any particular way, it might just happen to (not) work.
from react.
You're right this does look like a bug though.
from react.
Here's a minimal repro:
import { useState } from "react";
let ran = false;
export default function ReproduceBug() {
const [state, setState] = useState("a");
maybeRun(setState);
return <div>{state}</div>;
}
function maybeRun(setState) {
if (!ran) {
ran = true;
setTimeout(() => {
setState("b");
}, 500);
}
}
What seems to happen here is that setState
from the first StrictMode render is not capable of scheduling the update for some reason. So calls to it are ignored. However, in order to call it at all, you have to be breaking the rules of React (like in this repro, we're doing a side effect during rendering). If we were following the rules of React and either calling setState
from an event handler or from an effect (or even during rendering directly — no timeouts), then the code would work. Because event handlers and "effects" would see setState
from the second StrictMode render (which actually gets used):
export default function ReproduceBug() {
const [state, setState] = useState("a");
maybeRun(setState);
return <div onClick={() => setState("c")}>{state}</div>;
}
Note clicking the div "works" because it "sees" the setState
from the second StrictMode render (which got committed). You can verify that the setState
versions between two StrictMode renders are indeed different:
import { useState } from "react";
export default function ReproduceBug() {
const [state, setState] = useState("a");
maybeRun(setState);
return <div>{state}</div>;
}
let _setState;
function maybeRun(setState) {
if (_setState && _setState !== setState) {
throw Error("wat");
}
_setState = setState;
setTimeout(() => {
setState("b");
}, 500);
}
I'm not sure why (maybe this is a bug, maybe this is a known difference), but as long as you follow the rules of React, I think this should not be observable. Regardless, this does not seem to be happening with @next
versions. Maybe this means this was a bug that got fixed, or maybe it's a change to internal behavior. Since it breaks the rules in the first place, I'd say it's in the realm of undefined behavior.
from react.
I've bisected the behavior change to #25583. Seems like this was an intentional change done for a different reason, but with fixing this as a consequence. This test in particular seems like our case: https://github.com/facebook/react/pull/25583/files#diff-19df471970763c4790c2cc0811fd2726cc6a891b0e1d5dedbf6d0599240c127aR456-R482
So I think we can close this as fixed. But we don't strictly consider this a bugfix because the behavior for breaking the rules is kind of undefined.
from react.
I have to agree in the sense that I cannot reproduce this problem without breaking the rules. Any further discussion would be out of scope for a bug ticket. Thank you very much!
from react.
Related Issues (20)
- Feature Request: Reset form inside of Server Component or Action HOT 10
- Bug: submitting form clears fields unnecessarily despite using preventDefault HOT 3
- Bug: the <textarea> component's ValidityState.tooShort is incorrect when component is controlled and the value is passed in via the value prop HOT 2
- Why react-dom/server will automatic reorder elements in head
- useRef: Counting renders doesn´t work as expected in React neither 18.0.1 nor 18.0.2 HOT 2
- Bug:
- The React Framework has been one of the best in terms of creating responsive and exciting web design. Its development environment has been helpful in terms of dependencies and how quickly they are accessible by downloading in the terminal using 'npm.' I'm glad I could use the great framework this year. Cheers to a great year, and here comes 2024! HOT 2
- KBug:
- [DevTools Bug]: Hook parsing fails if a hook uses useSyncExternalStore HOT 9
- Bug: After some time useSyncExternalStore stops reacting on store updates HOT 1
- Bug: Memory Leak in React 17 when using useRef Hook HOT 1
- Why doesn't react absorb some of the awesome designs from the community, is it because they don't want to or won't?
- Replace "Donate to Ukraine" Banner with "Support Palestine" in ReactJS HOT 23
- Bug: Suspense Exception in react-server-dom-webpack HOT 2
- Bug: Can't submit form programatically when using server actions HOT 7
- [DevTools Bug] Cannot add node "20" because a node with that id is already in the Store. HOT 1
- [DevTools Bug] Cannot read properties of undefined (reading 'concat') HOT 4
- Bug: Random preloads added for images HOT 3
- Bug: Weird behavior of form when i set a state value within an onChange handler. HOT 3
- Content-Security-Policy: The page’s settings blocked the loading of a resource at inline (“script-src”). HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.