# Table of Contents - [React Reference Overview – React](#react-reference-overview-react) - [useActionState – React](#useactionstate-react) - [Built-in React Hooks – React](#built-in-react-hooks-react) - [useDebugValue – React](#usedebugvalue-react) - [useCallback – React](#usecallback-react) - [useContext – React](#usecontext-react) - [useDeferredValue – React](#usedeferredvalue-react) - [useEffect – React](#useeffect-react) - [useId – React](#useid-react) - [useImperativeHandle – React](#useimperativehandle-react) - [useInsertionEffect – React](#useinsertioneffect-react) - [useOptimistic – React](#useoptimistic-react) - [useReducer – React](#usereducer-react) - [useMemo – React](#usememo-react) - [useRef – React](#useref-react) - [useState – React](#usestate-react) - [useSyncExternalStore – React](#usesyncexternalstore-react) - [Built-in React Components – React](#built-in-react-components-react) - [useTransition – React](#usetransition-react) - [ (<>...) – React](#-fragment-react) - [ – React](#-profiler-react) - [ – React](#-strictmode-react) - [createContext – React](#createcontext-react) - [lazy – React](#lazy-react) --- # React Reference Overview – React [API Reference](/reference/react) React Reference Overview[](#undefined "Link for this heading") =============================================================== This section provides detailed reference documentation for working with React. For an introduction to React, please visit the [Learn](/learn) section. The React reference documentation is broken down into functional subsections: React[](#react "Link for React ") ---------------------------------- Programmatic React features: * [Hooks](/reference/react/hooks) - Use different React features from your components. * [Components](/reference/react/components) - Built-in components that you can use in your JSX. * [APIs](/reference/react/apis) - APIs that are useful for defining components. * [Directives](/reference/rsc/directives) - Provide instructions to bundlers compatible with React Server Components. React DOM[](#react-dom "Link for React DOM ") ---------------------------------------------- React-dom contains features that are only supported for web applications (which run in the browser DOM environment). This section is broken into the following: * [Hooks](/reference/react-dom/hooks) - Hooks for web applications which run in the browser DOM environment. * [Components](/reference/react-dom/components) - React supports all of the browser built-in HTML and SVG components. * [APIs](/reference/react-dom) - The `react-dom` package contains methods supported only in web applications. * [Client APIs](/reference/react-dom/client) - The `react-dom/client` APIs let you render React components on the client (in the browser). * [Server APIs](/reference/react-dom/server) - The `react-dom/server` APIs let you render React components to HTML on the server. Rules of React[](#rules-of-react "Link for Rules of React ") ------------------------------------------------------------- React has idioms — or rules — for how to express patterns in a way that is easy to understand and yields high-quality applications: * [Components and Hooks must be pure](/reference/rules/components-and-hooks-must-be-pure) – Purity makes your code easier to understand, debug, and allows React to automatically optimize your components and hooks correctly. * [React calls Components and Hooks](/reference/rules/react-calls-components-and-hooks) – React is responsible for rendering components and hooks when necessary to optimize the user experience. * [Rules of Hooks](/reference/rules/rules-of-hooks) – Hooks are defined using JavaScript functions, but they represent a special type of reusable UI logic with restrictions on where they can be called. Legacy APIs[](#legacy-apis "Link for Legacy APIs ") ---------------------------------------------------- * [Legacy APIs](/reference/react/legacy) - Exported from the `react` package, but not recommended for use in newly written code. [NextHooks](/reference/react/hooks) * * * --- # useActionState – React [API Reference](/reference/react) [Hooks](/reference/react/hooks) useActionState[](#undefined "Link for this heading") ===================================================== `useActionState` is a Hook that allows you to update state based on the result of a form action. const [state, formAction, isPending] = useActionState(fn, initialState, permalink?); ### Note In earlier React Canary versions, this API was part of React DOM and called `useFormState`. * [Reference](#reference) * [`useActionState(action, initialState, permalink?)`](#useactionstate) * [Usage](#usage) * [Using information returned by a form action](#using-information-returned-by-a-form-action) * [Troubleshooting](#troubleshooting) * [My action can no longer read the submitted form data](#my-action-can-no-longer-read-the-submitted-form-data) * * * Reference[](#reference "Link for Reference ") ---------------------------------------------- ### `useActionState(action, initialState, permalink?)`[](#useactionstate "Link for this heading") Call `useActionState` at the top level of your component to create component state that is updated [when a form action is invoked](/reference/react-dom/components/form) . You pass `useActionState` an existing form action function as well as an initial state, and it returns a new action that you use in your form, along with the latest form state and whether the Action is still pending. The latest form state is also passed to the function that you provided. import { useActionState } from "react";async function increment(previousState, formData) { return previousState + 1;}function StatefulForm({}) { const [state, formAction] = useActionState(increment, 0); return (
{state}
)} The form state is the value returned by the action when the form was last submitted. If the form has not yet been submitted, it is the initial state that you pass. If used with a Server Function, `useActionState` allows the server’s response from submitting the form to be shown even before hydration has completed. [See more examples below.](#usage) #### Parameters[](#parameters "Link for Parameters ") * `fn`: The function to be called when the form is submitted or button pressed. When the function is called, it will receive the previous state of the form (initially the `initialState` that you pass, subsequently its previous return value) as its initial argument, followed by the arguments that a form action normally receives. * `initialState`: The value you want the state to be initially. It can be any serializable value. This argument is ignored after the action is first invoked. * **optional** `permalink`: A string containing the unique page URL that this form modifies. For use on pages with dynamic content (eg: feeds) in conjunction with progressive enhancement: if `fn` is a [server function](/reference/rsc/server-functions) and the form is submitted before the JavaScript bundle loads, the browser will navigate to the specified permalink URL, rather than the current page’s URL. Ensure that the same form component is rendered on the destination page (including the same action `fn` and `permalink`) so that React knows how to pass the state through. Once the form has been hydrated, this parameter has no effect. #### Returns[](#returns "Link for Returns ") `useActionState` returns an array with the following values: 1. The current state. During the first render, it will match the `initialState` you have passed. After the action is invoked, it will match the value returned by the action. 2. A new action that you can pass as the `action` prop to your `form` component or `formAction` prop to any `button` component within the form. The action can also be called manually within [`startTransition`](/reference/react/startTransition) . 3. The `isPending` flag that tells you whether there is a pending Transition. #### Caveats[](#caveats "Link for Caveats ") * When used with a framework that supports React Server Components, `useActionState` lets you make forms interactive before JavaScript has executed on the client. When used without Server Components, it is equivalent to component local state. * The function passed to `useActionState` receives an extra argument, the previous or initial state, as its first argument. This makes its signature different than if it were used directly as a form action without using `useActionState`. * * * Usage[](#usage "Link for Usage ") ---------------------------------- ### Using information returned by a form action[](#using-information-returned-by-a-form-action "Link for Using information returned by a form action ") Call `useActionState` at the top level of your component to access the return value of an action from the last time a form was submitted. import { useActionState } from 'react';import { action } from './actions.js';function MyComponent() { const [state, formAction] = useActionState(action, null); // ... return (
{/* ... */}
);} `useActionState` returns an array with the following items: 1. The current state of the form, which is initially set to the initial state you provided, and after the form is submitted is set to the return value of the action you provided. 2. A new action that you pass to `
` as its `action` prop or call manually within `startTransition`. 3. A pending state that you can utilise while your action is processing. When the form is submitted, the action function that you provided will be called. Its return value will become the new current state of the form. The action that you provide will also receive a new first argument, namely the current state of the form. The first time the form is submitted, this will be the initial state you provided, while with subsequent submissions, it will be the return value from the last time the action was called. The rest of the arguments are the same as if `useActionState` had not been used. function action(currentState, formData) { // ... return 'next state';} #### Display information after submitting a form[](#display-information-after-submitting-a-form "Link for Display information after submitting a form") 1. Display form errors 2. Display structured information after submitting a form #### Example 1 of 2: Display form errors[](#display-form-errors "Link for this heading") To display messages such as an error message or toast that’s returned by a Server Function, wrap the action in a call to `useActionState`. App.jsactions.js App.js Reset[Fork](https://codesandbox.io/api/v1/sandboxes/define?undefined&environment=create-react-app "Open in CodeSandbox") import { useActionState, useState } from "react"; import { addToCart } from "./actions.js"; function AddToCartForm({itemID, itemTitle}) { const \[message, formAction, isPending\] = useActionState(addToCart, null); return ( {itemTitle} );} Now any `Button` inside of the provider will receive the current `theme` value. If you call `setTheme` to update the `theme` value that you pass to the provider, all `Button` components will re-render with the new `'light'` value. #### Examples of updating context[](#examples-basic "Link for Examples of updating context") 1. Updating a value via context 2. Updating an object via context 3. Multiple contexts 4. Extracting providers to a component 5. Scaling up with context and a reducer #### Example 1 of 5: Updating a value via context[](#updating-a-value-via-context "Link for this heading") In this example, the `MyApp` component holds a state variable which is then passed to the `ThemeContext` provider. Checking the “Dark mode” checkbox updates the state. Changing the provided value re-renders all the components using that context. App.js App.js Reset[Fork](https://codesandbox.io/api/v1/sandboxes/define?undefined&environment=create-react-app "Open in CodeSandbox") import { createContext, useContext, useState } from 'react'; const ThemeContext = createContext(null); export default function MyApp() { const \[theme, setTheme\] = useState('light'); return ( { setTheme(e.target.checked ? 'dark' : 'light') }} /> Use dark mode ) } function Form({ children }) { return ( Sign up Log in ); } function Panel({ title, children }) { const theme = useContext(ThemeContext); const className = 'panel-' + theme; return (
{title} {children} ) } function Button({ children }) { const theme = useContext(ThemeContext); const className = 'button-' + theme; return (