reactjs / reactjs/react.dev

Problem with recommending startTransition in design system event handlers?

未关闭
#2,604 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

we generally recommend to bake useTransition into the design system components of your app
https://reactjs.org/docs/concurrent-mode-patterns.html#baking-transitions-into-the-design-system

 // Button component in design system event handler:

  function handleClick() {
    startTransition(() => {
      onClick();
    });
  }

Couldn't this lead to the problem mentioned later?

Try typing into the input now. Something’s wrong! The input is updating very slowly
https://reactjs.org/docs/concurrent-mode-patterns.html#splitting-high-and-low-priority-state

The answer to this problem is to split the state in two parts: a “high priority” part that updates immediately, and a “low priority” part that may wait for a transition.

Solution:

function handleChange(e) {
  const value = e.target.value;
  
  // Outside the transition (urgent)
  setQuery(value);

  startTransition(() => {
    // Inside the transition (may be delayed)
    setResource(fetchTranslation(value));
  });
}

If your design system components wrap all their event handlers in startTransition then how would you make a setState call in your handler run outside of a transition?

贡献指南

打开贡献指南

从这里开始

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

调研方向

查看链接的 Concurrent Mode Patterns 部分,了解如何将 transitions 融入 design-system components,以及如何拆分高优先级和低优先级的 state。以 issue 中的 event-handler 示例作为起点;完成的标准是文档清楚地说明 design-system handlers 是否应当将 callbacks 包裹在 startTransition 中,以及如何让紧急更新保持在 transition 之外。

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

评估

技术栈
javascript, react
领域
documentation
Issue 类型
文档
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
30/100

把新 issue 发到你的邮箱

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