[Suggestion]: Adding documentation to note that Effects in children components are committed before their parent component
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- JavaScript
- Sterne
- 11.8k
- Forks
- 7.9k
- Ø Merge
- 1 T. 11 Std.
- Gemergte PRs (30 T.)
- 11
Beschreibung
Summary
Adding documentation to the useEffect page to note that Effects in children components are committed before their parent component
Page
https://react.dev/reference/react/useEffect
Details
Hi
I was digging into React internals and learning how it works under the hood. I came across an interesting behaviour in Effects -- where the Effects in children components are committed before their parent component.
For example:
function Parent({ children }) {
useEffect(() => {
console.log("Parent committed effect");
}, []);
return <div>{children}</div>;
}
function Child() {
useEffect(() => {
console.log("Child committed effect");
}, []);
return <p>Child</p>;
}
export default function App() {
return (
<Parent>
<Child />
</Parent>
);
}
The result in the console will be
Child committed effect
Parent committed effect
(I made a small animation describing the traversal)
I understand that it is due to how React traverses the Fiber tree during the Commit phase (recursivelyTraversePassiveMountEffects), but it seems like it does confuse some people in
- https://github.com/facebook/react/issues/15281
- https://www.reddit.com/r/reactjs/comments/1447nft/how_the_useeffect_work_in_react/
This is not a bug, but I think it would be helpful to have this documented, perhaps in the Caveats or the Troubleshooting section to avoid confusion around this "unexpected behaviour".
What do you think? :)
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der useEffect-Referenzseite, insbesondere mit den Abschnitten Caveats und Troubleshooting, und prüfe anschließend zur Einordnung die verknüpfte ReactFiberCommitWork.js-Traversierung. Als abgeschlossen gilt die Aufgabe, wenn auf der Seite klar dokumentiert ist, dass untergeordnete Effects vor übergeordneten Effects committed werden, wobei das gemeldete Beispiel verwendet wird und jeder Eindruck vermieden wird, dass dieses Verhalten ein Fehler ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, react
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 50/100