reactjs / reactjs/react.dev

[Suggestion]: Fix the `data.js` file part of the solution for Challenge 4 in "Choosing the State Structure" chapter

Aperta Adatta ai principianti
#7,394 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

type: documentation
Lingua principale
JavaScript
Stelle
11.8k
Fork
7.9k
Merge medio
1g 11h
PR unite (30g)
11

Descrizione

Summary

The data.js provided in the solution for "Challenge 4" has a property called isStarred which is unused in the provided solution for the challenge:

export const letters = [{
  id: 0,
  subject: 'Ready for adventure?',
-  isStarred: true,
}, {

Thus my suggestion is to remove the isStarred property from data.js file in challenge 4

Page

https://react.dev/learn/choosing-the-state-structure#recap

Details

The presence of the unused isStarred property can potentially confuse a beginner following the documentation, because they might have gone through the section on Avoiding Redundant States. I mean to say that they might end up writing a solution which utilises the isStarred property like shown below, which also solves the challenge:

import { useState } from 'react';
+import { letters as initialLetters } from './data.js';
import Letter from './Letter.js';

export default function MailClient() {
+  const [letters, setLetters] = useState(initialLetters);

+  const selectedCount = letters.filter(({isStarred}) => isStarred).length;

  function handleToggle(toggledId) {
+    setLetters(letters => letters.map(letter => {
+     if (letter.id === toggledId) {
+        return { 
+         ...letter,
+          isStarred: !letter.isStarred
+       }
+      } else return letter;
+   }))
  }

  return (
    <>
      <h2>Inbox</h2>
      <ul>
        {letters.map(letter => (
          <Letter
            key={letter.id}
            letter={letter}
+           isSelected={letter.isStarred}
            onToggle={handleToggle}
          />
        ))}
        <hr />
        <p>
          <b>
            You selected {selectedCount} letters
          </b>
        </p>
      </ul>
    </>
  );
}

Although the above solution works, it has the following cons:

  • Tight Coupling: Overloading isStarred for both "selected" and "starred" behaviours creates coupling between two potentially distinct concepts. If the app later needs to treat "starred" and "selected" as separate attributes, refactoring will be necessary.
  • Side Effects: Modifying isStarred might have unintended consequences elsewhere in the app if other features or components depend on it strictly representing "starred" status.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Apri la soluzione di Challenge 4 collegata dalla pagina Choosing the State Structure ed esamina il suo file data.js. Conferma che isStarred non venga utilizzato dalla soluzione fornita, quindi verifica che la challenge visualizzata e il riepilogo rimangano coerenti dopo la rimozione della proprietà inutilizzata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, react
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
1/5
Tempo stimato
Meno di un'ora
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
70/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.