Skip to content

Commit 9b3f1f3

Browse files
committed
Ensure dispatch in useFormState works in StrictMode
1 parent c2f1a2c commit 9b3f1f3

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

packages/react-dom/src/__tests__/ReactDOMForm-test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,6 @@ describe('ReactDOMForm', () => {
12961296
expect(container.textContent).toBe('B');
12971297
});
12981298

1299-
13001299
// @gate enableFormActions
13011300
// @gate enableAsyncActions
13021301
test('useFormState works in StrictMode', async () => {
@@ -1326,15 +1325,20 @@ describe('ReactDOMForm', () => {
13261325
}
13271326

13281327
const root = ReactDOMClient.createRoot(container);
1329-
await act(() => root.render(<React.StrictMode><App /></React.StrictMode>));
1328+
await act(() =>
1329+
root.render(
1330+
<React.StrictMode>
1331+
<App />
1332+
</React.StrictMode>,
1333+
),
1334+
);
13301335
assertLog(['0']);
13311336
expect(container.textContent).toBe('0');
13321337

13331338
await act(() => dispatch('increment'));
13341339
assertLog(['Async action started [1]', 'Pending 0']);
13351340
expect(container.textContent).toBe('Pending 0');
13361341

1337-
13381342
await act(() => resolveText('Wait [1]'));
13391343
assertLog(['1']);
13401344
expect(container.textContent).toBe('1');

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,9 @@ function rerenderFormState<S, P>(
22592259
);
22602260
}
22612261

2262+
// For mount, pending is always false.
2263+
const [isPending] = rerenderState(false);
2264+
22622265
// This is a mount. No updates to process.
22632266
const state: Awaited<S> = stateHook.memoizedState;
22642267

@@ -2269,8 +2272,7 @@ function rerenderFormState<S, P>(
22692272
// This may have changed during the rerender.
22702273
actionQueueHook.memoizedState = action;
22712274

2272-
// For mount, pending is always false.
2273-
return [state, dispatch, false];
2275+
return [state, dispatch, isPending];
22742276
}
22752277

22762278
function pushEffect(

0 commit comments

Comments
 (0)