Skip to content

Commit 8ef14cf

Browse files
eps1lonacdlite
andauthored
Ensure dispatch from useFormState works in StrictMode (#28557)
Co-authored-by: Andrew Clark <[email protected]>
1 parent 5cec48e commit 8ef14cf

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,4 +1295,52 @@ describe('ReactDOMForm', () => {
12951295
assertLog(['B']);
12961296
expect(container.textContent).toBe('B');
12971297
});
1298+
1299+
// @gate enableFormActions
1300+
// @gate enableAsyncActions
1301+
test('useFormState works in StrictMode', async () => {
1302+
let actionCounter = 0;
1303+
async function action(state, type) {
1304+
actionCounter++;
1305+
1306+
Scheduler.log(`Async action started [${actionCounter}]`);
1307+
await getText(`Wait [${actionCounter}]`);
1308+
1309+
switch (type) {
1310+
case 'increment':
1311+
return state + 1;
1312+
case 'decrement':
1313+
return state - 1;
1314+
default:
1315+
return state;
1316+
}
1317+
}
1318+
1319+
let dispatch;
1320+
function App() {
1321+
const [state, _dispatch, isPending] = useFormState(action, 0);
1322+
dispatch = _dispatch;
1323+
const pending = isPending ? 'Pending ' : '';
1324+
return <Text text={pending + state} />;
1325+
}
1326+
1327+
const root = ReactDOMClient.createRoot(container);
1328+
await act(() =>
1329+
root.render(
1330+
<React.StrictMode>
1331+
<App />
1332+
</React.StrictMode>,
1333+
),
1334+
);
1335+
assertLog(['0']);
1336+
expect(container.textContent).toBe('0');
1337+
1338+
await act(() => dispatch('increment'));
1339+
assertLog(['Async action started [1]', 'Pending 0']);
1340+
expect(container.textContent).toBe('Pending 0');
1341+
1342+
await act(() => resolveText('Wait [1]'));
1343+
assertLog(['1']);
1344+
expect(container.textContent).toBe('1');
1345+
});
12981346
});

packages/react-reconciler/src/ReactFiberHooks.js

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

2262+
updateWorkInProgressHook(); // State
2263+
22622264
// This is a mount. No updates to process.
22632265
const state: Awaited<S> = stateHook.memoizedState;
22642266

0 commit comments

Comments
 (0)