Running an effect only on update vs Strict mode
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 7.9k
- Merge medio
- 1g 11h
- PR unite (30g)
- 11
Descrizione
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!
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo il comportamento nei due esempi CodeSandbox collegati, confrontando i casi con Strict Mode e senza Strict Mode. Leggi la documentazione attuale di useEffect e useRef insieme al vecchio estratto delle FAQ; il lavoro è completo quando il comportamento consigliato per gli effetti che saltano il rendering iniziale è documentato chiaramente oppure l’issue è spiegata come comportamento previsto di Strict Mode.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, react
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100