[Bug]: The solution to the state-related challenges on the webpage appear to be incorrect.
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 45/100
- Type d'issue
- Bug
- Clarté
- Plutôt claire
- Activité
- À l'abandon
- Stack technique
- javascript, react
- Domaine
- documentation
Piste de recherche
Ouvrez la page Updating Arrays in State et reproduisez le Challenge 2 avec l’exemple App.js fourni, en comparant la sortie de la console aux compteurs affichés. Déterminez si la solution ou son explication est incorrecte ; la tâche est terminée lorsque le challenge documenté produit un état et des résultats affichés cohérents.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Summary
In the documentation on the 'Learn' page, under the section 'Updating Arrays in State,' the solution for Challenge 2 appears to have a bug.
When using console.log to debug the solution, the counts in the product array do not match what is displayed on the screen.
Page
https://react.dev/learn/updating-arrays-in-state
Details
"According to the solution's description, when the '+' button is pressed, the count value in the initialProducts array should increment. However, logging the values to the console shows that the values on the screen differ from those in the array."
Solution with 2 extra lines for logging (App.js):
import { useState } from 'react';
const initialProducts = [{
id: 0,
name: 'Baklava',
count: 1,
}, {
id: 1,
name: 'Cheese',
count: 5,
}, {
id: 2,
name: 'Spaghetti',
count: 2,
}];
export default function ShoppingCart() {
const [
products,
setProducts
] = useState(initialProducts)
function handleIncreaseClick(productId) {
setProducts(products.map(product => {
if (product.id === productId) {
return {
...product,
count: product.count + 1
};
} else {
return product;
}
}))
// Next 2 lines are not parts of the solution.
// They were added to test the result.
console.log(initialProducts)
console.log(products)
}
function handleDecreaseClick(productId) {
let nextProducts = products.map(product => {
if (product.id === productId) {
return {
...product,
count: product.count - 1
};
} else {
return product;
}
});
nextProducts = nextProducts.filter(p =>
p.count > 0
);
setProducts(nextProducts)
}
return (
<ul>
{products.map(product => (
<li key={product.id}>
{product.name}
{' '}
(<b>{product.count}</b>)
<button onClick={() => {
handleIncreaseClick(product.id);
}}>
+
</button>
<button onClick={() => {
handleDecreaseClick(product.id);
}}>
–
</button>
</li>
))}
</ul>
);
}
- Langage dominant
- JavaScript
- Étoiles
- 11.8k
- Forks
- 7.9k
- Merge moyen
- 16 h 6 min
- PR mergées (30 j)
- 7
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.
Autres issues de reactjs/react.dev
-
type: documentation
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 82/100
-
bug: unconfirmed
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
-
type: typos
Difficulté 1/5 Moins d'une heure Accessibilité débutants 90/100
-
bug: unconfirmed
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
Toutes les issues de reactjs/react.dev
Issues similaires
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
avniproject/avni-client#2135 ·
-
enhancement
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
babalae/bettergi-scripts-list#3674 ·
-
A-Release-Notes C-Editing D-Modest S-Ready-For-Implementation
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
bevyengine/bevy-website#2595 ·
-
ecosystem wording
Difficulté 1/5 Moins d'une heure Accessibilité débutants 90/100
matrix-org/matrix.org#3649 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
vadimdemedes/ink#1029 ·