GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix: useDeferredValue should reuse previous value (#24413) · react/react@bd08137

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/react-reconciler/src
    • ReactFiberHooks.new.js
    • ReactFiberHooks.old.js
    • __tests__
      • ReactDeferredValue-test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/react-reconciler/src/ReactFiberHooks.new.js‎

Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHooks.new.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ function updateDeferredValueImpl<T>(hook: Hook, prevValue: T, value: T): T {
19931993
markWorkInProgressReceivedUpdate();
19941994
}
19951995

1996+
hook.memoizedState = value;
19961997
return value;
19971998
}
19981999
}
Collapse file

‎packages/react-reconciler/src/ReactFiberHooks.old.js‎

Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHooks.old.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ function updateDeferredValueImpl<T>(hook: Hook, prevValue: T, value: T): T {
19931993
markWorkInProgressReceivedUpdate();
19941994
}
19951995

1996+
hook.memoizedState = value;
19961997
return value;
19971998
}
19981999
}
Collapse file

‎packages/react-reconciler/src/__tests__/ReactDeferredValue-test.js‎

Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactDeferredValue-test.js
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,79 @@ describe('ReactDeferredValue', () => {
221221
</div>,
222222
);
223223
});
224+
225+
it('regression test: during urgent update, reuse previous value, not initial value', async () => {
226+
function App({value: propValue}) {
227+
const [value, setValue] = useState(null);
228+
if (value !== propValue) {
229+
setValue(propValue);
230+
}
231+
232+
const deferredValue = useDeferredValue(value);
233+
234+
const child = useMemo(() => <Text text={'Original: ' + value} />, [
235+
value,
236+
]);
237+
238+
const deferredChild = useMemo(
239+
() => <Text text={'Deferred: ' + deferredValue} />,
240+
[deferredValue],
241+
);
242+
243+
return (
244+
<div>
245+
<div>{child}</div>
246+
<div>{deferredChild}</div>
247+
</div>
248+
);
249+
}
250+
251+
const root = ReactNoop.createRoot();
252+
253+
// Initial render
254+
await act(async () => {
255+
root.render(<App value={1} />);
256+
expect(Scheduler).toFlushUntilNextPaint(['Original: 1', 'Deferred: 1']);
257+
expect(root).toMatchRenderedOutput(
258+
<div>
259+
<div>Original: 1</div>
260+
<div>Deferred: 1</div>
261+
</div>,
262+
);
263+
});
264+
265+
await act(async () => {
266+
startTransition(() => {
267+
root.render(<App value={2} />);
268+
});
269+
expect(Scheduler).toFlushUntilNextPaint(['Original: 2', 'Deferred: 2']);
270+
expect(root).toMatchRenderedOutput(
271+
<div>
272+
<div>Original: 2</div>
273+
<div>Deferred: 2</div>
274+
</div>,
275+
);
276+
});
277+
278+
await act(async () => {
279+
root.render(<App value={3} />);
280+
// In the regression, the memoized value was not updated during non-urgent
281+
// updates, so this would flip the deferred value back to the initial
282+
// value (1) instead of reusing the current one (2).
283+
expect(Scheduler).toFlushUntilNextPaint(['Original: 3']);
284+
expect(root).toMatchRenderedOutput(
285+
<div>
286+
<div>Original: 3</div>
287+
<div>Deferred: 2</div>
288+
</div>,
289+
);
290+
expect(Scheduler).toFlushUntilNextPaint(['Deferred: 3']);
291+
expect(root).toMatchRenderedOutput(
292+
<div>
293+
<div>Original: 3</div>
294+
<div>Deferred: 3</div>
295+
</div>,
296+
);
297+
});
298+
});
224299
});

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.