Skip to content

Commit aa774f4

Browse files
committed
Fix up memoization of useTransition
1 parent cc2b341 commit aa774f4

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

integration/rendering-test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ test.describe("rendering", () => {
99
let appFixture: AppFixture;
1010

1111
test.beforeAll(async () => {
12-
process.env.NODE_ENV = "development";
1312
fixture = await createFixture({
1413
files: {
1514
"app/root.jsx": js`

integration/transition-state-test.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ test.describe("rendering", () => {
3232
export default function() {
3333
const transition = useTransition();
3434
35-
// TODO: This seems to have some issues with StrictMode?
36-
// - Passes running only transition tests w/StrictMode
37-
// - Passes on full suite with fullyParallel:false w/StrictMode
38-
// - Fails on full suite with fullyParallel:true w/StrictMode
39-
// - Passes on full suite with fullyParallel:true w/o StrictMode
4035
const transitionsRef = useRef();
4136
const transitions = useMemo(() => {
4237
const savedTransitions = transitionsRef.current || [];
@@ -47,6 +42,7 @@ test.describe("rendering", () => {
4742
4843
return (
4944
<html lang="en">
45+
<head><title>Test</title></head>
5046
<body>
5147
<Outlet />
5248
{transition.state != "idle" && (

packages/remix-react/components.tsx

+17-4
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ import(${JSON.stringify(manifest.entry.module)});`;
796796
/>
797797
<script
798798
{...props}
799+
suppressHydrationWarning
799800
dangerouslySetInnerHTML={createHtml(routeModulesScript)}
800801
type="module"
801802
async
@@ -957,10 +958,22 @@ export function useActionData<T = AppData>(): SerializeFrom<T> | undefined {
957958
*/
958959
export function useTransition(): Transition {
959960
let navigation = useNavigation();
960-
return React.useMemo(
961-
() => convertNavigationToTransition(navigation),
962-
[navigation]
963-
);
961+
962+
// Have to avoid useMemo here to avoid introducing unstable transition object
963+
// identities in StrictMode, since navigation will be stable but using
964+
// [navigation] as the dependency array will _still_ re-run on concurrent
965+
// renders, and that will create a new object identify for transition
966+
let lastNavigationRef = React.useRef<Navigation>();
967+
let lastTransitionRef = React.useRef<Transition>();
968+
969+
if (lastTransitionRef.current && lastNavigationRef.current === navigation) {
970+
return lastTransitionRef.current;
971+
}
972+
973+
lastNavigationRef.current = navigation;
974+
lastTransitionRef.current = convertNavigationToTransition(navigation);
975+
976+
return lastTransitionRef.current;
964977
}
965978

966979
function convertNavigationToTransition(navigation: Navigation): Transition {

0 commit comments

Comments
 (0)