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 摘要。