reactjs / reactjs/react.dev

Clock examples use impure `.toLocaleTimeString()`

未关闭
#5,136 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
11.8k
派生
7.9k
平均合并
1 天 11 小时
30 天内合并 PR
11

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

在新的 React Docs 中搜索 Clock 示例,并查看“Keeping Components Pure”挑战以及链接的 Clock.js 示例。更新这些示例,使 Clock 接收语言上下文,同时保留本地化输出,然后验证页面上的所有 Clock 示例都遵循相同的模式。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, react
领域
documentation, frontend
Issue 类型
文档
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。