From 265fa26e3b39739f06c956140d9acf618c6b4e6b Mon Sep 17 00:00:00 2001 From: Matt Michel Date: Sun, 3 Mar 2024 22:26:51 +0000 Subject: [PATCH 1/2] Update small typo in useDeferredValue.md (#6673) * Update useDeferredValue.md change text from "re-render in background" to "re-render in the background" * Update useDeferredValue.md Change instances of "in background" to "in the background". --- src/content/reference/react/useDeferredValue.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/content/reference/react/useDeferredValue.md b/src/content/reference/react/useDeferredValue.md index dcd8f98d0b..f97bf0a281 100644 --- a/src/content/reference/react/useDeferredValue.md +++ b/src/content/reference/react/useDeferredValue.md @@ -40,7 +40,7 @@ function SearchPage() { #### Returns {/*returns*/} -During the initial render, the returned deferred value will be the same as the value you provided. During updates, React will first attempt a re-render with the old value (so it will return the old value), and then try another re-render in background with the new value (so it will return the updated value). +During the initial render, the returned deferred value will be the same as the value you provided. During updates, React will first attempt a re-render with the old value (so it will return the old value), and then try another re-render in the background with the new value (so it will return the updated value). #### Caveats {/*caveats*/} @@ -76,7 +76,7 @@ function SearchPage() { During the initial render, the deferred value will be the same as the value you provided. -During updates, the deferred value will "lag behind" the latest value. In particular, React will first re-render *without* updating the deferred value, and then try to re-render with the newly received value in background. +During updates, the deferred value will "lag behind" the latest value. In particular, React will first re-render *without* updating the deferred value, and then try to re-render with the newly received value in the background. **Let's walk through an example to see when this is useful.** @@ -508,7 +508,7 @@ You can think of it as happening in two steps: 1. **First, React re-renders with the new `query` (`"ab"`) but with the old `deferredQuery` (still `"a")`.** The `deferredQuery` value, which you pass to the result list, is *deferred:* it "lags behind" the `query` value. -2. **In background, React tries to re-render with *both* `query` and `deferredQuery` updated to `"ab"`.** If this re-render completes, React will show it on the screen. However, if it suspends (the results for `"ab"` have not loaded yet), React will abandon this rendering attempt, and retry this re-render again after the data has loaded. The user will keep seeing the stale deferred value until the data is ready. +2. **In the background, React tries to re-render with *both* `query` and `deferredQuery` updated to `"ab"`.** If this re-render completes, React will show it on the screen. However, if it suspends (the results for `"ab"` have not loaded yet), React will abandon this rendering attempt, and retry this re-render again after the data has loaded. The user will keep seeing the stale deferred value until the data is ready. The deferred "background" rendering is interruptible. For example, if you type into the input again, React will abandon it and restart with the new value. React will always use the latest provided value. @@ -952,7 +952,7 @@ While these techniques are helpful in some cases, `useDeferredValue` is better s Unlike debouncing or throttling, it doesn't require choosing any fixed delay. If the user's device is fast (e.g. powerful laptop), the deferred re-render would happen almost immediately and wouldn't be noticeable. If the user's device is slow, the list would "lag behind" the input proportionally to how slow the device is. -Also, unlike with debouncing or throttling, deferred re-renders done by `useDeferredValue` are interruptible by default. This means that if React is in the middle of re-rendering a large list, but the user makes another keystroke, React will abandon that re-render, handle the keystroke, and then start rendering in background again. By contrast, debouncing and throttling still produce a janky experience because they're *blocking:* they merely postpone the moment when rendering blocks the keystroke. +Also, unlike with debouncing or throttling, deferred re-renders done by `useDeferredValue` are interruptible by default. This means that if React is in the middle of re-rendering a large list, but the user makes another keystroke, React will abandon that re-render, handle the keystroke, and then start rendering in the background again. By contrast, debouncing and throttling still produce a janky experience because they're *blocking:* they merely postpone the moment when rendering blocks the keystroke. If the work you're optimizing doesn't happen during rendering, debouncing and throttling are still useful. For example, they can let you fire fewer network requests. You can also use these techniques together. From 268ec99309983d03469a257c64140b989e45fdbc Mon Sep 17 00:00:00 2001 From: Yucohny <79147654+Yucohny@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:15:06 +0800 Subject: [PATCH 2/2] fix: resolve conflict --- src/content/reference/react/useDeferredValue.md | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/content/reference/react/useDeferredValue.md b/src/content/reference/react/useDeferredValue.md index c55d81e5ed..a5fcd2fe66 100644 --- a/src/content/reference/react/useDeferredValue.md +++ b/src/content/reference/react/useDeferredValue.md @@ -40,11 +40,7 @@ function SearchPage() { #### 返回值 {/*returns*/} -<<<<<<< HEAD 在组件的初始渲染期间,返回的延迟值将与你提供的值相同。但是在组件更新时,React 将会先尝试使用旧值进行重新渲染(因此它将返回旧值),然后再在后台使用新值进行另一个重新渲染(这时它将返回更新后的值)。 -======= -During the initial render, the returned deferred value will be the same as the value you provided. During updates, React will first attempt a re-render with the old value (so it will return the old value), and then try another re-render in the background with the new value (so it will return the updated value). ->>>>>>> 265fa26e3b39739f06c956140d9acf618c6b4e6b #### 注意事项 {/*caveats*/} @@ -80,11 +76,7 @@ function SearchPage() { 在初始渲染期间,返回的 延迟值 与你提供的 相同。 -<<<<<<< HEAD 在更新期间,延迟值 会“滞后于”最新的 。具体地说,React 首先会在不更新延迟值的情况下进行重新渲染,然后在后台尝试使用新接收到的值进行重新渲染。 -======= -During updates, the deferred value will "lag behind" the latest value. In particular, React will first re-render *without* updating the deferred value, and then try to re-render with the newly received value in the background. ->>>>>>> 265fa26e3b39739f06c956140d9acf618c6b4e6b **让我们通过一个例子来看看什么时候该使用它**。 @@ -516,11 +508,7 @@ input { margin: 10px; } 1. **首先,React 会使用新的 `query` 值(`"ab"`)和旧的 `deferredQuery` 值(仍为 `"a"`)重新渲染。** 传递给结果列表的 `deferredQuery` 值是**延迟**的,它“滞后于” `query` 值。 -<<<<<<< HEAD 2. **在后台,React 尝试重新渲染,并将 `query` 和 `deferredQuery` 两个值都更新为 `"ab"`。** 如果此次重新渲染完成,React 将在屏幕上显示它。但是,如果它 suspense(即 `"ab"` 的结果尚未加载),React 将放弃这次渲染,并在数据加载后再次尝试重新渲染。用户将一直看到旧的延迟值,直到数据准备就绪。 -======= -2. **In the background, React tries to re-render with *both* `query` and `deferredQuery` updated to `"ab"`.** If this re-render completes, React will show it on the screen. However, if it suspends (the results for `"ab"` have not loaded yet), React will abandon this rendering attempt, and retry this re-render again after the data has loaded. The user will keep seeing the stale deferred value until the data is ready. ->>>>>>> 265fa26e3b39739f06c956140d9acf618c6b4e6b 被推迟的“后台”渲染是可中断的。例如,如果你再次在输入框中输入,React 将会中断渲染,并从新值开始重新渲染。React 总是使用最新提供的值。 @@ -964,11 +952,7 @@ export default SlowList; 与防抖或节流不同,`useDeferredValue` 不需要选择任何固定延迟时间。如果用户的设备很快(比如性能强劲的笔记本电脑),延迟的重渲染几乎会立即发生并且不会被察觉。如果用户的设备较慢,那么列表会相应地“滞后”于输入,滞后的程度与设备的速度有关。 -<<<<<<< HEAD 此外,与防抖或节流不同,`useDeferredValue` 执行的延迟重新渲染默认是可中断的。这意味着,如果 React 正在重新渲染一个大型列表,但用户进行了另一次键盘输入,React 会放弃该重新渲染,先处理键盘输入,然后再次开始在后台渲染。相比之下,防抖和节流仍会产生不顺畅的体验,因为它们是阻塞的:它们仅仅是将渲染阻塞键盘输入的时刻推迟了。 -======= -Also, unlike with debouncing or throttling, deferred re-renders done by `useDeferredValue` are interruptible by default. This means that if React is in the middle of re-rendering a large list, but the user makes another keystroke, React will abandon that re-render, handle the keystroke, and then start rendering in the background again. By contrast, debouncing and throttling still produce a janky experience because they're *blocking:* they merely postpone the moment when rendering blocks the keystroke. ->>>>>>> 265fa26e3b39739f06c956140d9acf618c6b4e6b 如果你要优化的工作不是在渲染期间发生的,那么防抖和节流仍然非常有用。例如,它们可以让你减少网络请求的次数。你也可以同时使用这些技术。