Skip to content
Merged
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
Add stub for experimental_useFormStatus
This wires up, but does not yet implement, an experimental hook called
useFormStatus. The hook is imported from React DOM, not React, because
it represents DOM-specific state — its return type includes FormData as
one of its fields. Other renderers that implement similar methods would
use their own renderer-specific types.

The API is prefixed and only available in the experimental channel.

It can only be used from client (browser, SSR) components, not
Server Components.
  • Loading branch information
acdlite committed Apr 24, 2023
commit 663e13af050bf2b9568ad5e5d935550db7856fc2
1 change: 1 addition & 0 deletions packages/react-dom/index.classic.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
unstable_createEventHandle,
unstable_renderSubtreeIntoContainer,
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
useFormStatus as experimental_useFormStatus,
Copy link
Collaborator

@sebmarkbage sebmarkbage Apr 24, 2023

Choose a reason for hiding this comment

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

Just noticing that the form flag is on in www but not the async transitions one so it's kind of a useless combination for this export.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I intentionally kept the async one off because it affects error handling behavior even in regular, sync useTransition. Should we turn enableFormActions off too?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yea, let's do that for now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we turn them on we have to also update this to get the right external runtime from experimental or make an fb build.

https://github.com/facebook/react/blob/main/.github/workflows/commit_artifacts.yml#L137-L138

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok I opened a separate PR where we can discuss: #26721

prefetchDNS,
preconnect,
preload,
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/index.experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
unstable_batchedUpdates,
unstable_renderSubtreeIntoContainer,
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
useFormStatus as experimental_useFormStatus,
prefetchDNS,
preconnect,
preload,
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
unstable_createEventHandle,
unstable_renderSubtreeIntoContainer,
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
useFormStatus as experimental_useFormStatus,
prefetchDNS,
preconnect,
preload,
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/index.modern.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
unstable_batchedUpdates,
unstable_createEventHandle,
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
useFormStatus as experimental_useFormStatus,
prefetchDNS,
preconnect,
preload,
Expand Down
50 changes: 50 additions & 0 deletions packages/react-dom/src/ReactDOMFormActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import {enableAsyncActions, enableFormActions} from 'shared/ReactFeatureFlags';

type FormStatusNotPending = {|
pending: false,
data: null,
method: null,
action: null,
|};

type FormStatusPending = {|
pending: true,
data: FormData,
method: string,
action: string | (FormData => void | Promise<void>),
|};

export type FormStatus = FormStatusPending | FormStatusNotPending;

// Since the "not pending" value is always the same, we can reuse the
// same object across all transitions.
const sharedNotPendingObject = {
pending: false,
data: null,
method: null,
action: null,
};

const NotPending: FormStatus = __DEV__
? Object.freeze(sharedNotPendingObject)
: sharedNotPendingObject;

export function useFormStatus(): FormStatus {
if (!(enableFormActions && enableAsyncActions)) {
throw new Error('Not implemented.');
} else {
// TODO: This isn't fully implemented yet but we return a correctly typed
// value so we can test that the API is exposed and gated correctly. The
// real implementation will access the status via the dispatcher.
return NotPending;
}
}
18 changes: 18 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFizzForm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ let container;
let React;
let ReactDOMServer;
let ReactDOMClient;
let useFormStatus;

describe('ReactDOMFizzForm', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOMServer = require('react-dom/server.browser');
ReactDOMClient = require('react-dom/client');
useFormStatus = require('react-dom').experimental_useFormStatus;
act = require('internal-test-utils').act;
container = document.createElement('div');
document.body.appendChild(container);
Expand Down Expand Up @@ -360,4 +362,20 @@ describe('ReactDOMFizzForm', () => {
expect(buttonRef.current.hasAttribute('formMethod')).toBe(false);
expect(buttonRef.current.hasAttribute('formTarget')).toBe(false);
});

// @gate enableFormActions
// @gate enableAsyncActions
it('useFormStatus is not pending during server render', async () => {
function App() {
const {pending} = useFormStatus();
return 'Pending: ' + pending;
}

const stream = await ReactDOMServer.renderToReadableStream(<App />);
await readIntoContainer(stream);
expect(container.textContent).toBe('Pending: false');

await act(() => ReactDOMClient.hydrateRoot(container, <App />));
expect(container.textContent).toBe('Pending: false');
});
});
18 changes: 18 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMForm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('ReactDOMForm', () => {
let Suspense;
let startTransition;
let textCache;
let useFormStatus;

beforeEach(() => {
jest.resetModules();
Expand All @@ -51,6 +52,7 @@ describe('ReactDOMForm', () => {
useState = React.useState;
Suspense = React.Suspense;
startTransition = React.startTransition;
useFormStatus = ReactDOM.experimental_useFormStatus;
container = document.createElement('div');
document.body.appendChild(container);

Expand Down Expand Up @@ -846,4 +848,20 @@ describe('ReactDOMForm', () => {
assertLog(['Oh no!', 'Oh no!']);
expect(container.textContent).toBe('Oh no!');
});

// @gate enableFormActions
// @gate enableAsyncActions
it('useFormStatus exists', async () => {
// This API isn't fully implemented yet. This just tests that it's wired
// up correctly.

function App() {
const {pending} = useFormStatus();
return 'Pending: ' + pending;
}

const root = ReactDOMClient.createRoot(container);
await act(() => root.render(<App />));
expect(container.textContent).toBe('Pending: false');
});
});
1 change: 1 addition & 0 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
import Internals from '../ReactDOMSharedInternals';

export {prefetchDNS, preconnect, preload, preinit} from '../ReactDOMFloat';
export {useFormStatus} from '../ReactDOMFormActions';

if (__DEV__) {
if (
Expand Down