Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMForm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1295,4 +1295,52 @@ describe('ReactDOMForm', () => {
assertLog(['B']);
expect(container.textContent).toBe('B');
});

// @gate enableFormActions
// @gate enableAsyncActions
test('useFormState works in StrictMode', async () => {
let actionCounter = 0;
async function action(state, type) {
actionCounter++;

Scheduler.log(`Async action started [${actionCounter}]`);
await getText(`Wait [${actionCounter}]`);

switch (type) {
case 'increment':
return state + 1;
case 'decrement':
return state - 1;
default:
return state;
}
}

let dispatch;
function App() {
const [state, _dispatch, isPending] = useFormState(action, 0);
dispatch = _dispatch;
const pending = isPending ? 'Pending ' : '';
return <Text text={pending + state} />;
}

const root = ReactDOMClient.createRoot(container);
await act(() =>
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
),
);
assertLog(['0']);
expect(container.textContent).toBe('0');

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

await act(() => resolveText('Wait [1]'));
assertLog(['1']);
expect(container.textContent).toBe('1');
});
});
6 changes: 4 additions & 2 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,9 @@ function rerenderFormState<S, P>(
);
}

// For mount, pending is always false.
const [isPending] = rerenderState(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we don't actually need the result, all we need to do is advance the hook list.

Suggested change
const [isPending] = rerenderState(false);
updateWorkInProgressHook();

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a bit brittle but you know this part better than I do.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consider this pretty low level code so I think it's fine. I care more about not observing the value of isPending since it causes a bunch of redundant read operations.


// This is a mount. No updates to process.
const state: Awaited<S> = stateHook.memoizedState;

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

// For mount, pending is always false.
return [state, dispatch, false];
return [state, dispatch, isPending];
}

function pushEffect(
Expand Down