Running an effect only on update vs Strict mode
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
I have written a variation of the useEffect hook that should only run on updates. I used a useRef hook to accomplish this the way the FAQ in the old docs recommends it here:
I couldn't find the same piece in the new documentation.
The problem is that the effect still runs on the initial render when strict mode is on:
https://codesandbox.io/s/delicate-lake-88phjz?file=/src/App.js
however it behaves correctly when the strict mode is off:
https://codesandbox.io/s/hungry-wright-tl7p43?file=/src/App.js
In the former example the message is logged to the console right away even though it shouldn't be.
The possible explanations I can think of are:
- I'm doing something wrong. Can you please point the mistake out for me?
- The old docs are off and this is not the right way to do it. In this case can you please let me know what the right way of achieving this behavior is?
- The strict mode is to blame. I do find it irritating at times, if I'm frank, but that's probably unlikely.
Can someone help me with this? Thanks a lot!
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 damit, das Verhalten in den beiden verlinkten CodeSandbox-Beispielen zu reproduzieren, und vergleiche dabei die Fälle mit Strict Mode und ohne Strict Mode. Lies die aktuelle Dokumentation zu useEffect und useRef zusammen mit dem alten FAQ-Auszug; als erledigt gilt die Aufgabe, wenn das empfohlene Verhalten für Effekte, die den initialen Render überspringen, klar dokumentiert ist oder das Issue als erwartetes Strict Mode-Verhalten erklärt wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, react
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100