Loading...
Loading...
Comprehensive MDX component patterns (Note, Pitfall, DeepDive, Recipes, etc.) for all documentation types. Authoritative source for component usage, examples, and heading conventions.
npx skill4agent add reactjs/react.dev docs-components| Need | Component |
|---|---|
| Helpful tip or terminology | |
| Common mistake warning | |
| Advanced technical explanation | |
| Canary-only feature | |
| Server Components only | |
| Deprecated API | |
| Experimental/WIP | |
| Visual diagram | |
| Multiple related examples | |
| Interactive code | |
| Console error display | |
| End-of-page exercises | |
| Component | Heading Level |
|---|---|
| DeepDive title | |
| Titled Pitfall | |
| Titled Note | |
| Recipe items | |
| Challenge items | |
<Pitfall><Pitfall><Note><Note><DeepDive><DeepDive><Pitfall><DeepDive><Pitfall>
Don't do X.
</Pitfall>
<Pitfall>
Don't do Y.
</Pitfall><Pitfall>
##### Don't do X {/*pitfall-x*/}
Explanation.
##### Don't do Y {/*pitfall-y*/}
Explanation.
</Pitfall><Pitfall>
Don't do X.
</Pitfall>
This leads to another common mistake:
<Pitfall>
Don't do Y.
</Pitfall><Note><Note>
The optimization of caching return values is known as [_memoization_](https://en.wikipedia.org/wiki/Memoization).
</Note>####<Note>
#### There is no directive for Server Components. {/*no-directive*/}
A common misunderstanding is that Server Components are denoted by `"use server"`, but there is no directive for Server Components. The `"use server"` directive is for Server Functions.
</Note><Note>
Starting in React 19, you can render `<SomeContext>` as a provider.
In older versions of React, use `<SomeContext.Provider>`.
</Note><Pitfall><Pitfall>
We recommend defining components as functions instead of classes. [See how to migrate.](#alternatives)
</Pitfall>#####<Pitfall>
##### Calling different memoized functions will read from different caches. {/*pitfall-different-caches*/}
To access the same cache, components must call the same memoized function.
</Pitfall><Pitfall>
##### `useFormStatus` will not return status information for a `<form>` rendered in the same component. {/*pitfall-same-component*/}
```js
function Form() {
// 🔴 `pending` will never be true
const { pending } = useFormStatus();
return <form action={submit}></form>;
}useFormStatus<form><DeepDive>####<DeepDive>
#### Is using an updater always preferred? {/*is-updater-preferred*/}
You might hear a recommendation to always write code like `setAge(a => a + 1)` if the state you're setting is calculated from the previous state. There's no harm in it, but it's also not always necessary.
In most cases, there is no difference between these two approaches. React always makes sure that for intentional user actions, like clicks, the `age` state variable would be updated before the next click.
</DeepDive><DeepDive>
#### When should I use `cache`, `memo`, or `useMemo`? {/*cache-memo-usememo*/}
All mentioned APIs offer memoization but differ in what they memoize, who can access the cache, and when their cache is invalidated.
#### `useMemo` {/*deep-dive-usememo*/}
In general, you should use `useMemo` for caching expensive computations in Client Components across renders.
#### `cache` {/*deep-dive-cache*/}
In general, you should use `cache` in Server Components to memoize work that can be shared across components.
</DeepDive><Recipes><Solution /><Recipes titleText="Basic useState examples" titleId="examples-basic">
#### Counter (number) {/*counter-number*/}
In this example, the `count` state variable holds a number.
<Sandpack>
{/* code */}
</Sandpack>
<Solution />
#### Text field (string) {/*text-field-string*/}
In this example, the `text` state variable holds a string.
<Sandpack>
{/* code */}
</Sandpack>
<Solution />
</Recipes>examples-basicexamples-[concept]examples-[topic]<Challenges><Challenges>
#### Fix the bug {/*fix-the-bug*/}
Problem description...
<Hint>
Optional hint text.
</Hint>
<Sandpack>
{/* problem code */}
</Sandpack>
<Solution>
Explanation...
<Sandpack>
{/* solution code */}
</Sandpack>
</Solution>
</Challenges>####<Deprecated><Deprecated>
In React 19, `forwardRef` is no longer necessary. Pass `ref` as a prop instead.
`forwardRef` will be deprecated in a future release. Learn more [here](/blog/2024/04/25/react-19#ref-as-a-prop).
</Deprecated>### `componentWillMount()` {/*componentwillmount*/}
<Deprecated>
This API has been renamed from `componentWillMount` to [`UNSAFE_componentWillMount`.](#unsafe_componentwillmount)
Run the [`rename-unsafe-lifecycles` codemod](codemod-link) to automatically update.
</Deprecated><RSC><RSC>
`cache` is only for use with [React Server Components](/reference/rsc/server-components).
</RSC><RSC>
Server Functions are for use in [React Server Components](/reference/rsc/server-components).
**Note:** Until September 2024, we referred to all Server Functions as "Server Actions".
</RSC><Canary><CanaryBadge /><Intro>
`<Fragment>` lets you group elements without a wrapper node.
<Canary>Fragments can also accept refs, enabling interaction with underlying DOM nodes.</Canary>
</Intro>### <CanaryBadge /> FragmentInstance {/*fragmentinstance*/}* <CanaryBadge /> **optional** `ref`: A ref object from `useRef` or callback function.* <CanaryBadge /> If you want to pass `ref` to a Fragment, you can't use the `<>...</>` syntax.<Diagram><Diagram name="use_client_module_dependency" height={250} width={545} alt="A tree graph with the top node representing the module 'App.js'. 'App.js' has three children...">
`'use client'` segments the module dependency tree, marking `InspirationGenerator.js` and all dependencies as client-rendered.
</Diagram>nameheightwidthalt<CodeStep>```js [[1, 4, "age"], [2, 4, "setAge"], [3, 4, "42"]]
import { useState } from 'react';
function MyComponent() {
const [age, setAge] = useState(42);
}
Format: `[[step_number, line_number, "text_to_highlight"], ...]`
In prose:
```mdx
1. The <CodeStep step={1}>current state</CodeStep> initially set to the <CodeStep step={3}>initial value</CodeStep>.
2. The <CodeStep step={2}>`set` function</CodeStep> that lets you change it.React will compare the <CodeStep step={2}>dependencies</CodeStep> with the dependencies you passed...When an <CodeStep step={1}>Activity</CodeStep> boundary is <CodeStep step={2}>hidden</CodeStep> during its <CodeStep step={3}>initial</CodeStep> render...<ConsoleBlock><ConsoleBlock level="error">
Uncaught Error: Too many re-renders.
</ConsoleBlock>errorwarninginfo/docs-writer-reference<RSC><Intro><Deprecated><Intro><Deprecated><Canary><Intro><CanaryBadge />/docs-writer-learn/docs-writer-blog<Experimental><ExperimentalBadge /><RSCBadge /><NextMajor><Wip><DiagramGroup><Illustration><IllustrationBlock><CodeDiagram><FullWidth><ConsoleBlockMulti><ConsoleLogLine><TerminalBlock><BlogCard><TeamMember><YouTubeIframe><ErrorDecoder /><LearnMore><Math><MathI><LanguageList>