Running an effect only on update vs Strict mode
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- JavaScript
- Estrelas
- 11.8k
- Forks
- 7.9k
- Merge médio
- 1d 11h
- PRs com merge (30d)
- 11
Descrição
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!
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece reproduzindo o comportamento nos dois exemplos vinculados do CodeSandbox, comparando os casos com Strict Mode e sem Strict Mode. Leia a documentação atual de useEffect e useRef junto com o trecho antigo das FAQ; considera-se concluído quando o comportamento recomendado para efeitos que ignoram a renderização inicial estiver claramente documentado ou quando o issue for explicado como um comportamento esperado do Strict Mode.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- javascript, react
- Domínio
- documentation
- Tipo de issue
- Documentação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Status de atividade
- Estagnada
- Clareza
- Precisa de esclarecimento
- Facilidade para iniciantes
- 25/100