Questionable Comment in the `useEvent` Example
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 found a paragraph from Separating Events from Effects in the beta React Docs, which I couldn't wrap my head around:
In this example,
urlinsideonVisitcorresponds to the latesturl(which could have already changed), butvisitedUrlcorresponds to theurlthat originally caused this Effect (and thisonVisitcall) to run.
The mentioned example is effectively the one below:
function Page({ url }) {
const { items } = useContext(ShoppingCartContext);
const numberOfItems = items.length;
const onVisit = useEvent(visitedUrl => {
// `url` captured here is claimed to be the latest, not the original
// but what about `numberOfItems`?
logVisit(visitedUrl, numberOfItems);
});
// intend to log the `url` that originally caused this Effect,
// along with the original `numberOfItems`.
useEffect(() => {
setTimeout(() => {
onVisit(url);
}, 5000); // Delay logging visits
}, [url]);
// ...
}
More essentially, I believe my confusion stems from the claims below:
This solves the problem. Similar to the
setfunctions returned fromuseState, all Event functions are stable: they never change on a re-render. This is why you can skip them in the dependency list. They are not reactive.
How can Event functions like onVisit are stable, when they need to capture the relevant reactive values like numberOfItems?
Are they stable, but have access to the freshly-constructed anonymous function, which captures the relevant reactive values?
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 pelo exemplo “Separating Events from Effects” vinculado na issue e compare as afirmações sobre a estabilidade de useEvent com os valores exibidos numberOfItems e url. Está concluído quando a documentação esclarecer claramente quais valores o callback atrasado observa e explicar por que a função de evento pode ser omitida das dependências.
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
- 5/5
- Tempo estimado
- Mais de uma semana
- Status de atividade
- Estagnada
- Clareza
- Precisa de esclarecimento
- Facilidade para iniciantes
- 25/100