# React > `act`is a test helper to apply pending React updates before making assertions. --- # Source: https://react.dev/reference/react/act --- title: act --- `act` is a test helper to apply pending React updates before making assertions. ```js await act(async actFn) ``` To prepare a component for assertions, wrap the code rendering it and performing updates inside an `await act()` call. This makes your test run closer to how React works in the browser. You might find using `act()` directly a bit too verbose. To avoid some of the boilerplate, you could use a library like [React Testing Library](https://testing-library.com/docs/react-testing-library/intro), whose helpers are wrapped with `act()`. --- ## Reference {/*reference*/} ### `await act(async actFn)` {/*await-act-async-actfn*/} When writing UI tests, tasks like rendering, user events, or data fetching can be considered as “units” of interaction with a user interface. React provides a helper called `act()` that makes sure all updates related to these “units” have been processed and applied to the DOM before you make any assertions. The name `act` comes from the [Arrange-Act-Assert](https://wiki.c2.com/?ArrangeActAssert) pattern. ```js {2,4} it ('renders with button disabled', async () => { await act(async () => { root.render() }); expect(container.querySelector('button')).toBeDisabled(); }); ``` We recommend using `act` with `await` and an `async` function. Although the sync version works in many cases, it doesn't work in all cases and due to the way React schedules updates internally, it's difficult to predict when you can use the sync version. We will deprecate and remove the sync version in the future. #### Parameters {/*parameters*/} * `async actFn`: An async function wrapping renders or interactions for components being tested. Any updates triggered within the `actFn`, are added to an internal act queue, which are then flushed together to process and apply any changes to the DOM. Since it is async, React will also run any code that crosses an async boundary, and flush any updates scheduled. #### Returns {/*returns*/} `act` does not return anything. ## Usage {/*usage*/} When testing a component, you can use `act` to make assertions about its output. For example, let’s say we have this `Counter` component, the usage examples below show how to test it: ```js function Counter() { const [count, setCount] = useState(0); const handleClick = () => { setCount(prev => prev + 1); } useEffect(() => { document.title = `You clicked ${count} times`; }, [count]); return (

You clicked {count} times

) } ``` ### Rendering components in tests {/*rendering-components-in-tests*/} To test the render output of a component, wrap the render inside `act()`: ```js {10,12} import {act} from 'react'; import ReactDOMClient from 'react-dom/client'; import Counter from './Counter'; it('can render and update a counter', async () => { container = document.createElement('div'); document.body.appendChild(container); // ✅ Render the component inside act(). await act(() => { ReactDOMClient.createRoot(container).render(); }); const button = container.querySelector('button'); const label = container.querySelector('p'); expect(label.textContent).toBe('You clicked 0 times'); expect(document.title).toBe('You clicked 0 times'); }); ``` Here, we create a container, append it to the document, and render the `Counter` component inside `act()`. This ensures that the component is rendered and its effects are applied before making assertions. Using `act` ensures that all updates have been applied before we make assertions. ### Dispatching events in tests {/*dispatching-events-in-tests*/} To test events, wrap the event dispatch inside `act()`: ```js {14,16} import {act} from 'react'; import ReactDOMClient from 'react-dom/client'; import Counter from './Counter'; it.only('can render and update a counter', async () => { const container = document.createElement('div'); document.body.appendChild(container); await act( async () => { ReactDOMClient.createRoot(container).render(); }); // ✅ Dispatch the event inside act(). await act(async () => { button.dispatchEvent(new MouseEvent('click', { bubbles: true })); }); const button = container.querySelector('button'); const label = container.querySelector('p'); expect(label.textContent).toBe('You clicked 1 times'); expect(document.title).toBe('You clicked 1 times'); }); ``` Here, we render the component with `act`, and then dispatch the event inside another `act()`. This ensures that all updates from the event are applied before making assertions. Don’t forget that dispatching DOM events only works when the DOM container is added to the document. You can use a library like [React Testing Library](https://testing-library.com/docs/react-testing-library/intro) to reduce the boilerplate code. ## Troubleshooting {/*troubleshooting*/} ### I'm getting an error: "The current testing environment is not configured to support act(...)" {/*error-the-current-testing-environment-is-not-configured-to-support-act*/} Using `act` requires setting `global.IS_REACT_ACT_ENVIRONMENT=true` in your test environment. This is to ensure that `act` is only used in the correct environment. If you don't set the global, you will see an error like this: Warning: The current testing environment is not configured to support act(...) To fix, add this to your global setup file for React tests: ```js global.IS_REACT_ACT_ENVIRONMENT=true ``` In testing frameworks like [React Testing Library](https://testing-library.com/docs/react-testing-library/intro), `IS_REACT_ACT_ENVIRONMENT` is already set for you. --- # Source: https://react.dev/reference/react/Activity --- title: --- `` lets you hide and restore the UI and internal state of its children. ```js ``` --- ## Reference {/*reference*/} ### `` {/*activity*/} You can use Activity to hide part of your application: ```js [[1, 1, "\\"hidden\\""], [2, 2, ""], [3, 1, "\\"visible\\""]] ``` When an Activity boundary is hidden, React will visually hide its children using the `display: "none"` CSS property. It will also destroy their Effects, cleaning up any active subscriptions. While hidden, children still re-render in response to new props, albeit at a lower priority than the rest of the content. When the boundary becomes visible again, React will reveal the children with their previous state restored, and re-create their Effects. In this way, Activity can be thought of as a mechanism for rendering "background activity". Rather than completely discarding content that's likely to become visible again, you can use Activity to maintain and restore that content's UI and internal state, while ensuring that your hidden content has no unwanted side effects. [See more examples below.](#usage) #### Props {/*props*/} * `children`: The UI you intend to show and hide. * `mode`: A string value of either `'visible'` or `'hidden'`. If omitted, defaults to `'visible'`. #### Caveats {/*caveats*/} - If an Activity is rendered inside of a [ViewTransition](/reference/react/ViewTransition), and it becomes visible as a result of an update caused by [startTransition](/reference/react/startTransition), it will activate the ViewTransition's `enter` animation. If it becomes hidden, it will activate its `exit` animation. - An Activity that just renders text will not render anything rather than rendering hidden text, because there’s no corresponding DOM element to apply visibility changes to. For example, `` will not produce any output in the DOM for `const ComponentThatJustReturnsText = () => "Hello, World!"`. --- ## Usage {/*usage*/} ### Restoring the state of hidden components {/*restoring-the-state-of-hidden-components*/} In React, when you want to conditionally show or hide a component, you typically mount or unmount it based on that condition: ```jsx {isShowingSidebar && ( )} ``` But unmounting a component destroys its internal state, which is not always what you want. When you hide a component using an Activity boundary instead, React will "save" its state for later: ```jsx ``` This makes it possible to hide and then later restore components in the state they were previously in. The following example has a sidebar with an expandable section. You can press "Overview" to reveal the three subitems below it. The main app area also has a button that hides and shows the sidebar. Try expanding the Overview section, and then toggling the sidebar closed then open: ```js src/App.js active import { useState } from 'react'; import Sidebar from './Sidebar.js'; export default function App() { const [isShowingSidebar, setIsShowingSidebar] = useState(true); return ( <> {isShowingSidebar && ( )}

Main content

); } ``` ```js src/Sidebar.js import { useState } from 'react'; export default function Sidebar() { const [isExpanded, setIsExpanded] = useState(false) return ( ); } ``` ```css body { height: 275px; margin: 0; } #root { display: flex; gap: 10px; height: 100%; } nav { padding: 10px; background: #eee; font-size: 14px; height: 100%; } main { padding: 10px; } p { margin: 0; } h1 { margin-top: 10px; } .indicator { margin-left: 4px; display: inline-block; rotate: 90deg; } .indicator.down { rotate: 180deg; } ```
The Overview section always starts out collapsed. Because we unmount the sidebar when `isShowingSidebar` flips to `false`, all its internal state is lost. This is a perfect use case for Activity. We can preserve the internal state of our sidebar, even when visually hiding it. Let's replace the conditional rendering of our sidebar with an Activity boundary: ```jsx {7,9} // Before {isShowingSidebar && ( )} // After ``` and check out the new behavior: ```js src/App.js active import { Activity, useState } from 'react'; import Sidebar from './Sidebar.js'; export default function App() { const [isShowingSidebar, setIsShowingSidebar] = useState(true); return ( <>

Main content

); } ``` ```js src/Sidebar.js import { useState } from 'react'; export default function Sidebar() { const [isExpanded, setIsExpanded] = useState(false) return ( ); } ``` ```css body { height: 275px; margin: 0; } #root { display: flex; gap: 10px; height: 100%; } nav { padding: 10px; background: #eee; font-size: 14px; height: 100%; } main { padding: 10px; } p { margin: 0; } h1 { margin-top: 10px; } .indicator { margin-left: 4px; display: inline-block; rotate: 90deg; } .indicator.down { rotate: 180deg; } ```
Our sidebar's internal state is now restored, without any changes to its implementation. --- ### Restoring the DOM of hidden components {/*restoring-the-dom-of-hidden-components*/} Since Activity boundaries hide their children using `display: none`, their children's DOM is also preserved when hidden. This makes them great for maintaining ephemeral state in parts of the UI that the user is likely to interact with again. In this example, the Contact tab has a `

That's right!

``` Manipulating the UI imperatively works well enough for isolated examples, but it gets exponentially more difficult to manage in more complex systems. Imagine updating a page full of different forms like this one. Adding a new UI element or a new interaction would require carefully checking all existing code to make sure you haven't introduced a bug (for example, forgetting to show or hide something). React was built to solve this problem. In React, you don't directly manipulate the UI--meaning you don't enable, disable, show, or hide components directly. Instead, you **declare what you want to show,** and React figures out how to update the UI. Think of getting into a taxi and telling the driver where you want to go instead of telling them exactly where to turn. It's the driver's job to get you there, and they might even know some shortcuts you haven't considered! ## Thinking about UI declaratively {/*thinking-about-ui-declaratively*/} You've seen how to implement a form imperatively above. To better understand how to think in React, you'll walk through reimplementing this UI in React below: 1. **Identify** your component's different visual states 2. **Determine** what triggers those state changes 3. **Represent** the state in memory using `useState` 4. **Remove** any non-essential state variables 5. **Connect** the event handlers to set the state ### Step 1: Identify your component's different visual states {/*step-1-identify-your-components-different-visual-states*/} In computer science, you may hear about a ["state machine"](https://en.wikipedia.org/wiki/Finite-state_machine) being in one of several “states”. If you work with a designer, you may have seen mockups for different "visual states". React stands at the intersection of design and computer science, so both of these ideas are sources of inspiration. First, you need to visualize all the different "states" of the UI the user might see: * **Empty**: Form has a disabled "Submit" button. * **Typing**: Form has an enabled "Submit" button. * **Submitting**: Form is completely disabled. Spinner is shown. * **Success**: "Thank you" message is shown instead of a form. * **Error**: Same as Typing state, but with an extra error message. Just like a designer, you'll want to "mock up" or create "mocks" for the different states before you add logic. For example, here is a mock for just the visual part of the form. This mock is controlled by a prop called `status` with a default value of `'empty'`: ```js export default function Form({ status = 'empty' }) { if (status === 'success') { return

That's right!

} return ( <>

City quiz

In which city is there a billboard that turns air into drinkable water?

` is not allowed. [Use `defaultValue` for initial content.](#providing-an-initial-value-for-a-text-area) - If a text area receives a string `value` prop, it will be [treated as controlled.](#controlling-a-text-area-with-a-state-variable) - A text area can't be both controlled and uncontrolled at the same time. - A text area cannot switch between being controlled or uncontrolled over its lifetime. - Every controlled text area needs an `onChange` event handler that synchronously updates its backing value. --- ## Usage {/*usage*/} ### Displaying a text area {/*displaying-a-text-area*/} Render `` is not supported. --- ### Reading the text area value when submitting a form {/*reading-the-text-area-value-when-submitting-a-form*/} Add a [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) around your textarea with a [`