Docs regarding setState are in direct conflict with one another
Personne n'a encore pris cette issue.
- Langage dominant
- JavaScript
- Étoiles
- 11.8k
- Forks
- 7.9k
- Merge moyen
- 1 j 11 h
- PR mergées (30 j)
- 11
Description
In Introducting Hooks, and several other places, there is a simple counter example that illustrates how to use the useState hook.
function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
However, there happens to be another counter example in the Hooks API Reference for useState.
function Counter({initialCount}) {
const [count, setCount] = useState(initialCount);
return (
<>
Count: {count}
<button onClick={() => setCount(initialCount)}>Reset</button>
<button onClick={() => setCount(prevCount => prevCount - 1)}>-</button>
<button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
</>
);
}
In this second example, a callback is being passed into the setState function, and the code is accompanied by the following language...
The ”+” and ”-” buttons use the functional form, because the updated value is based on the previous value.
This would imply that the first example (which is in a very prominent location in the docs by the way), is actually incorrect and is encouraging an anti-pattern.
I hope you can see how these two examples are in direct conflict with one another and might be causing confusion in the community!
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Lisez les exemples de compteurs dans Introducing Hooks et Hooks API Reference, en particulier la section functional-updates, et comparez les indications concernant setState. Résolvez l’incohérence afin que la documentation fournisse une explication claire du moment où chaque forme est appropriée, puis vérifiez que les exemples liés et le texte environnant ne se contredisent plus.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, react
- Domaine
- documentation
- Type d'issue
- Documentation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100