reactjs / reactjs/react.dev

Clock examples use impure `.toLocaleTimeString()`

Offen
#5,136 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
JavaScript
Sterne
11.8k
Forks
7.9k
Ø Merge
1 T. 11 Std.
Gemergte PRs (30 T.)
11

Beschreibung

The new React Docs have various "Clock" examples and challenges throughout the page. All of them use .toLocaleTimeString() (without arguments!) to output the time. This is a bit strange, especially as an example on the page "Keeping Components Pure":

While .getHours() will always return the same value if the Date object is the same, the whole point of .toLocaleTimeString() with no locale specified is that it's impure, its return value depending on external state outside React: the configured environment locale.

If you scroll just a little bit upwards on the page "Keeping Components Pure", you'll find

Your components could run in a different environment—for example, on the server!

as one of the reasons for why React cares about component purity. But that example is very likely to break with .toLocaleTimeString(), as chances are that the server has a different default locale than the user's browser.

My suggestion would be to pass a language prop to the Clock component, so Clock itself is actually pure, but App (which isn't a visible part of the example) can do <Clock time={time} language={navigator.language} />, keeping the nice behavior of localized examples: https://codesandbox.io/s/sandpack-project-forked-43udl3?file=/Clock.js

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Durchsuche die neuen React Docs nach den Clock-Beispielen und überprüfe die Herausforderung „Keeping Components Pure“ zusammen mit dem verlinkten Clock.js-Beispiel. Aktualisiere die Beispiele so, dass Clock den Sprachkontext erhält, während die lokalisierte Ausgabe beibehalten wird, und überprüfe anschließend, dass alle Clock-Beispiele auf der Seite demselben Muster folgen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, react
Bereich
documentation, frontend
Issue-Typ
Dokumentation
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.