reactjs / reactjs/react.dev

Clock examples use impure `.toLocaleTimeString()`

Open
#5,136 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Search the new React Docs for the Clock examples and review the “Keeping Components Pure” challenge, along with the linked Clock.js example. Update the examples so Clock receives the language context while localized output is preserved, then verify that all Clock examples on the page follow the same pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation, frontend
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.