@devries-cameron20 / t3code Mirror · read-only / commits / f0dd86f164
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 15:45 UTC

fix(web): stable callbacks refresh before layout effects read them (#25489)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 26, 2026 at 06:09 UTC f0dd86f16482595c6cfffe0ac64dc12bffb634f9
2 files changed +32 -2
apps/web/src/hooks/useStableCallback.browser.test.tsx new
+28
@@ -0,0 +1,28 @@
1 +import { useLayoutEffect } from "react";
2 +import { describe, expect, it } from "vite-plus/test";
3 +
4 +import { render } from "../../test/render";
5 +import { useStableCallback } from "./useStableCallback";
6 +
7 +function Child({ value, onLayout }: { value: string; onLayout: (childValue: string) => void }) {
8 + useLayoutEffect(() => {
9 + onLayout(value);
10 + }, [value, onLayout]);
11 + return null;
12 +}
13 +
14 +function Parent({ value, seen }: { value: string; seen: string[] }) {
15 + const read = useStableCallback((childValue: string) => seen.push(`${childValue}->${value}`));
16 + return <Child value={value} onLayout={read} />;
17 +}
18 +
19 +describe("useStableCallback", () => {
20 + // Child layout effects run before the parent's, so a position restore in a
21 + // child must still reach the handler of the render it commits with.
22 + it("calls the committing render's function from a child layout effect", async () => {
23 + const seen: string[] = [];
24 + await using screen = await render(<Parent value="first" seen={seen} />, { router: false });
25 + await screen.rerender(<Parent value="second" seen={seen} />);
26 + expect(seen).toEqual(["first->first", "second->second"]);
27 + });
28 +});
apps/web/src/hooks/useStableCallback.ts
+4 -2
@@ -1,11 +1,13 @@
1 -import { useLayoutEffect, useRef, useState } from "react";
1 +import { useInsertionEffect, useRef, useState } from "react";
2
3 /** Call only from handlers and effects; render sees the previous commit's fn. */
4 export function useStableCallback<Args extends ReadonlyArray<unknown>, Result>(
5 fn: (...args: Args) => Result,
6 ): (...args: Args) => Result {
7 const latest = useRef(fn);
8 - useLayoutEffect(() => {
8 + // Insertion effects run before every layout effect in the commit, so a
9 + // child's layout effect already calls this render's fn.
10 + useInsertionEffect(() => {
11 latest.current = fn;
12 });
13 const [stable] = useState(