Skip to content

Clock examples use impure .toLocaleTimeString() #5136

@jonaskuske

Description

@jonaskuske

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions