Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Top-Level React APIs

React act() function

Reference for React act: wrap renders, events and async work in tests so state updates and effects are applied before you make assertions.

act is a testing helper. You wrap code that causes React updates in it (rendering, clicking, resolving a promise), and when act finishes, React has already processed every state update, run every effect and committed the DOM. Your assertions then see the final result instead of a half-updated screen.

Testing libraries call it for you (React Testing Library wraps render and fireEvent in act). You write it yourself when you drive React directly with createRoot or when you advance timers and promises by hand. Import it from react; older code imported it from react-dom/test-utils.

JSX
import { act } from 'react';

await act(async () => {
  root.render(<App />);
});

Try it

Render and click inside act
Async work and effects

The synchronous act flushes the render and the effect, but the promise inside the effect has not resolved yet, so it shows loading. The awaited version also waits for that promise and its state update, so it shows loaded.

Syntax

TypeScript
function act(callback: () => void | Promise<void>): Promise<void> & { then(...): void };
Name Description
callback A function that triggers updates. Synchronous or async.

Returns: a thenable. Always await it when the callback is async. You may skip await for a synchronous callback, but an async callback must be awaited, or React warns.

Environment flag: set globalThis.IS_REACT_ACT_ENVIRONMENT = true before testing. With it, React warns An update to X inside a test was not wrapped in act(...) when state changes outside act. Without it, those warnings are off and act itself warns that the environment is not configured.

Usage notes

  • Use the async form by default. await act(async () => { ... }) also waits for pending microtasks and effects, which is what most tests need.
  • It flushes React's work, not your timers. Fake timers (jest.useFakeTimers) must be advanced inside act so the resulting updates are flushed too.
  • Do not overlap calls. Await each act before starting the next; overlapping calls print a warning.
  • Use it only in tests. Production code never calls act.
  • Prefer a testing library when you can. React Testing Library's render, fireEvent, userEvent and waitFor use it correctly, so you seldom see it.
  • The warning is a hint, not noise. "not wrapped in act" means an update happened that your test did not account for, so assertions may run too early or an async effect leaks between tests.
  • Version note: act is exported from react in React 18.3 and 19; the old react-dom/test-utils import is deprecated.