Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React 19] When submitting a form action multiple times, requestFormReset throws the TypeError 'Cannot read null property (reading 'queue')'. #31478

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
57 changes: 57 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMForm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,63 @@ describe('ReactDOMForm', () => {
expect(inputRef.current.value).toBe('Initial');
});

it('submits once and then submits three times in quick succession', async () => {
const root = ReactDOMClient.createRoot(container);

const TestComponent = () => {
return (
<form
ref={formRef}
onSubmit={async e => {
e.preventDefault();
// Simulate an asynchronous operation that delays for 3 seconds
await new Promise(resolve => setTimeout(resolve, 3000));
}}>
<input type="text" name="name" ref={inputRef} />
<input type="submit" value="submit" />
</form>
);
};

const formRef = React.createRef();
const inputRef = React.createRef();
await act(() => root.render(<TestComponent />));

// Submit the form asynchronously once
await act(async () => {
await submit(formRef.current);
});

// Check the input value after the first submission
expect(inputRef.current.value).toBe('');

await act(() => {
startTransition(async () => {
// Submit the form.
await submit(formRef.current);
Scheduler.log('Action started');

// This happens after an `await`, and is not wrapped in startTransition,
// so it will be scheduled synchronously instead of with the transition.
// This is almost certainly a mistake, so we log a warning in dev.
requestFormReset(formRef.current);
Scheduler.log('Request form reset');
});
});

// Submit the form three more times in quick succession
for (let i = 0; i < 3; i++) {
await act(async () => {
await act(async () => {
await submit(formRef.current);
});
});
}

// Check the input value after the last submission
expect(inputRef.current.value).toBe('');
});

it("regression: submitter's formAction prop is coerced correctly before checking if it exists", async () => {
function App({submitterAction}) {
return (
Expand Down
7 changes: 6 additions & 1 deletion packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3200,7 +3200,7 @@ export function startHostTransition<F>(

function ensureFormComponentIsStateful(formFiber: Fiber) {
const existingStateHook: Hook | null = formFiber.memoizedState;
if (existingStateHook !== null) {
if (existingStateHook !== null && existingStateHook.next !== null) {
// This fiber was already upgraded to be stateful.
return existingStateHook;
}
Expand Down Expand Up @@ -3288,6 +3288,9 @@ export function requestFormReset(formFiber: Fiber) {
}

const stateHook = ensureFormComponentIsStateful(formFiber);
const isEmpty = stateHook === null || stateHook.length === 0;
if (isEmpty) return;

const newResetState = {};
const resetStateHook: Hook = (stateHook.next: any);
const resetStateQueue = resetStateHook.queue;
Expand Down Expand Up @@ -3537,6 +3540,8 @@ function dispatchSetStateInternal<S, A>(
action: A,
lane: Lane,
): boolean {
const isEmptyQueue = queue === null || queue.length === 0;
if (isEmptyQueue) return;
const update: Update<S, A> = {
lane,
revertLane: NoLane,
Expand Down