Skip to content

Commit 32d586d

Browse files
authored
Merge pull request #30 from reactjs/sync-a8790ca8
Sync with react.dev @ a8790ca
2 parents fd62885 + c72e984 commit 32d586d

17 files changed

+687
-177
lines changed
Loading
Loading
Loading
Loading

src/components/Layout/getRouteMeta.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ export interface RouteMeta {
5858
order?: number;
5959
}
6060

61-
type TravesalContext = RouteMeta & {
61+
type TraversalContext = RouteMeta & {
6262
currentIndex: number;
6363
};
6464

6565
export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) {
6666
const breadcrumbs = getBreadcrumbs(cleanedPath, routeTree);
67-
const ctx: TravesalContext = {
67+
const ctx: TraversalContext = {
6868
currentIndex: 0,
6969
};
7070
buildRouteMeta(cleanedPath, routeTree, ctx);
@@ -79,7 +79,7 @@ export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) {
7979
function buildRouteMeta(
8080
searchPath: string,
8181
currentRoute: RouteItem,
82-
ctx: TravesalContext
82+
ctx: TraversalContext
8383
) {
8484
ctx.currentIndex++;
8585

src/content/blog/2023/03/22/react-labs-what-we-have-been-working-on-march-2023.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Making plain JavaScript in React components reactive requires a compiler with a
7474

7575
## Offscreen Rendering {/*offscreen-rendering*/}
7676

77-
Offscreen rendering is an upcoming capability in React for rendering screens in the background without additional performance overhead. You can think of it as a version of the [`content-visiblity` CSS property](https://developer.mozilla.org/en-US/docs/Web/CSS/content-visibility) that works not only for DOM elements but React components, too. During our research, we've discovered a variety of use cases:
77+
Offscreen rendering is an upcoming capability in React for rendering screens in the background without additional performance overhead. You can think of it as a version of the [`content-visibility` CSS property](https://developer.mozilla.org/en-US/docs/Web/CSS/content-visibility) that works not only for DOM elements but React components, too. During our research, we've discovered a variety of use cases:
7878

7979
- A router can prerender screens in the background so that when a user navigates to them, they're instantly available.
8080
- A tab switching component can preserve the state of hidden tabs, so the user can switch between them without losing their progress.

src/content/learn/passing-data-deeply-with-context.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ export const places = [{
985985
}, {
986986
id: 5,
987987
name: 'Chefchaouen, Marocco',
988-
description: 'There are a few theories on why the houses are painted blue, including that the color repells mosquitos or that it symbolizes sky and heaven.',
988+
description: 'There are a few theories on why the houses are painted blue, including that the color repels mosquitos or that it symbolizes sky and heaven.',
989989
imageId: 'rTqKo46'
990990
}, {
991991
id: 6,
@@ -1124,7 +1124,7 @@ export const places = [{
11241124
}, {
11251125
id: 5,
11261126
name: 'Chefchaouen, Marocco',
1127-
description: 'There are a few theories on why the houses are painted blue, including that the color repells mosquitos or that it symbolizes sky and heaven.',
1127+
description: 'There are a few theories on why the houses are painted blue, including that the color repels mosquitos or that it symbolizes sky and heaven.',
11281128
imageId: 'rTqKo46'
11291129
}, {
11301130
id: 6,

src/content/reference/react-dom/components/form.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export async function deliverMessage(message) {
273273

274274
</Sandpack>
275275

276-
[//]: # 'Uncomment the next line, and delete this line after the `useOptimisitc` reference documentatino page is published'
276+
//]: # 'Uncomment the next line, and delete this line after the `useOptimistic` reference documentatino page is published'
277277
[//]: # 'To learn more about the `useOptimistic` Hook see the [reference documentation](/reference/react/hooks/useOptimistic).'
278278

279279
### Handling form submission errors {/*handling-form-submission-errors*/}

src/content/reference/react/Component.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ class Form extends Component {
632632
return (
633633
<>
634634
<input value={this.state.name} onChange={this.handleNameChange} />
635-
<p>Hello, {this.state.name}.
635+
<p>Hello, {this.state.name}.</p>
636636
</>
637637
);
638638
}

src/content/reference/react/cache.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ async function DemoProfile() {
309309
310310
React only provides cache access to the memoized function in a component. When calling <CodeStep step={1}>`getUser`</CodeStep> outside of a component, it will still evaluate the function but not read or update the cache.
311311
312-
This is because cache access is provided through a [context](/learn/passing-data-deeply-with-context) which is only accessibile from a component.
312+
This is because cache access is provided through a [context](/learn/passing-data-deeply-with-context) which is only accessible from a component.
313313
314314
</Pitfall>
315315

src/content/reference/react/directives.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ Directives provide instructions to [bundlers compatible with React Server Compon
1919

2020
## Source code directives {/*source-code-directives*/}
2121

22-
* [`'use client'`](/reference/react/use-client) marks source files whose components execute on the client.
22+
* [`'use client'`](/reference/react/use-client) lets you mark what code runs on the client.
2323
* [`'use server'`](/reference/react/use-server) marks server-side functions that can be called from client-side code.

src/content/reference/react/experimental_taintUniqueValue.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ In this example, the constant `password` is tainted. Then `password` is used to
131131

132132
Other similar ways of deriving new values from tainted values like concatenating it into a larger string, converting it to base64, or returning a substring create untained values.
133133

134-
Tainting only protects against simple mistakes like explictly passing secret values to the client. Mistakes in calling the `taintUniqueValue` like using a global store outside of React, without the corresponding lifetime object, can cause the tainted value to become untainted. Tainting is a layer of protection; a secure app will have multiple layers of protection, well designed APIs, and isolation patterns.
134+
Tainting only protects against simple mistakes like explicitly passing secret values to the client. Mistakes in calling the `taintUniqueValue` like using a global store outside of React, without the corresponding lifetime object, can cause the tainted value to become untainted. Tainting is a layer of protection; a secure app will have multiple layers of protection, well designed APIs, and isolation patterns.
135135

136136
</Pitfall>
137137

src/content/reference/react/hooks.md

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
title: "Built-in React Hooks"
3+
---
4+
5+
<Intro>
6+
7+
*Hooks* let you use different React features from your components. You can either use the built-in Hooks or combine them to build your own. This page lists all built-in Hooks in React.
8+
9+
</Intro>
10+
11+
---
12+
13+
## State Hooks {/*state-hooks*/}
14+
15+
*State* lets a component ["remember" information like user input.](/learn/state-a-components-memory) For example, a form component can use state to store the input value, while an image gallery component can use state to store the selected image index.
16+
17+
To add state to a component, use one of these Hooks:
18+
19+
* [`useState`](/reference/react/useState) declares a state variable that you can update directly.
20+
* [`useReducer`](/reference/react/useReducer) declares a state variable with the update logic inside a [reducer function.](/learn/extracting-state-logic-into-a-reducer)
21+
22+
```js
23+
function ImageGallery() {
24+
const [index, setIndex] = useState(0);
25+
// ...
26+
```
27+
28+
---
29+
30+
## Context Hooks {/*context-hooks*/}
31+
32+
*Context* lets a component [receive information from distant parents without passing it as props.](/learn/passing-props-to-a-component) For example, your app's top-level component can pass the current UI theme to all components below, no matter how deep.
33+
34+
* [`useContext`](/reference/react/useContext) reads and subscribes to a context.
35+
36+
```js
37+
function Button() {
38+
const theme = useContext(ThemeContext);
39+
// ...
40+
```
41+
42+
---
43+
44+
## Ref Hooks {/*ref-hooks*/}
45+
46+
*Refs* let a component [hold some information that isn't used for rendering,](/learn/referencing-values-with-refs) like a DOM node or a timeout ID. Unlike with state, updating a ref does not re-render your component. Refs are an "escape hatch" from the React paradigm. They are useful when you need to work with non-React systems, such as the built-in browser APIs.
47+
48+
* [`useRef`](/reference/react/useRef) declares a ref. You can hold any value in it, but most often it's used to hold a DOM node.
49+
* [`useImperativeHandle`](/reference/react/useImperativeHandle) lets you customize the ref exposed by your component. This is rarely used.
50+
51+
```js
52+
function Form() {
53+
const inputRef = useRef(null);
54+
// ...
55+
```
56+
57+
---
58+
59+
## Effect Hooks {/*effect-hooks*/}
60+
61+
*Effects* let a component [connect to and synchronize with external systems.](/learn/synchronizing-with-effects) This includes dealing with network, browser DOM, animations, widgets written using a different UI library, and other non-React code.
62+
63+
* [`useEffect`](/reference/react/useEffect) connects a component to an external system.
64+
65+
```js
66+
function ChatRoom({ roomId }) {
67+
useEffect(() => {
68+
const connection = createConnection(roomId);
69+
connection.connect();
70+
return () => connection.disconnect();
71+
}, [roomId]);
72+
// ...
73+
```
74+
75+
Effects are an "escape hatch" from the React paradigm. Don't use Effects to orchestrate the data flow of your application. If you're not interacting with an external system, [you might not need an Effect.](/learn/you-might-not-need-an-effect)
76+
77+
There are two rarely used variations of `useEffect` with differences in timing:
78+
79+
* [`useLayoutEffect`](/reference/react/useLayoutEffect) fires before the browser repaints the screen. You can measure layout here.
80+
* [`useInsertionEffect`](/reference/react/useInsertionEffect) fires before React makes changes to the DOM. Libraries can insert dynamic CSS here.
81+
82+
---
83+
84+
## Performance Hooks {/*performance-hooks*/}
85+
86+
A common way to optimize re-rendering performance is to skip unnecessary work. For example, you can tell React to reuse a cached calculation or to skip a re-render if the data has not changed since the previous render.
87+
88+
To skip calculations and unnecessary re-rendering, use one of these Hooks:
89+
90+
- [`useMemo`](/reference/react/useMemo) lets you cache the result of an expensive calculation.
91+
- [`useCallback`](/reference/react/useCallback) lets you cache a function definition before passing it down to an optimized component.
92+
93+
```js
94+
function TodoList({ todos, tab, theme }) {
95+
const visibleTodos = useMemo(() => filterTodos(todos, tab), [todos, tab]);
96+
// ...
97+
}
98+
```
99+
100+
Sometimes, you can't skip re-rendering because the screen actually needs to update. In that case, you can improve performance by separating blocking updates that must be synchronous (like typing into an input) from non-blocking updates which don't need to block the user interface (like updating a chart).
101+
102+
To prioritize rendering, use one of these Hooks:
103+
104+
- [`useTransition`](/reference/react/useTransition) lets you mark a state transition as non-blocking and allow other updates to interrupt it.
105+
- [`useDeferredValue`](/reference/react/useDeferredValue) lets you defer updating a non-critical part of the UI and let other parts update first.
106+
107+
---
108+
109+
## Resource Hooks {/*resource-hooks*/}
110+
111+
*Resources* can be accessed by a component without having them as part of their state. For example, a component can read a message from a Promise or read styling information from a context.
112+
113+
To read a value from a resource, use this Hook:
114+
115+
- [`use`](/reference/react/use) lets you read the value of a resource like a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
116+
117+
```js
118+
function MessageComponent({ messagePromise }) {
119+
const message = use(messagePromise);
120+
const theme = use(ThemeContext);
121+
// ...
122+
}
123+
```
124+
125+
---
126+
127+
## Other Hooks {/*other-hooks*/}
128+
129+
These Hooks are mostly useful to library authors and aren't commonly used in the application code.
130+
131+
- [`useDebugValue`](/reference/react/useDebugValue) lets you customize the label React DevTools displays for your custom Hook.
132+
- [`useId`](/reference/react/useId) lets a component associate a unique ID with itself. Typically used with accessibility APIs.
133+
- [`useSyncExternalStore`](/reference/react/useSyncExternalStore) lets a component subscribe to an external store.
134+
135+
---
136+
137+
## Your own Hooks {/*your-own-hooks*/}
138+
139+
You can also [define your own custom Hooks](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component) as JavaScript functions.

src/content/reference/react/index.md

+20-129
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,30 @@
11
---
2-
title: "Built-in React Hooks"
2+
title: React Reference Overview
33
---
44

55
<Intro>
6-
7-
*Hooks* let you use different React features from your components. You can either use the built-in Hooks or combine them to build your own. This page lists all built-in Hooks in React.
8-
6+
This section provides detailed reference documentation for working with React.
7+
For an introduction to React, please visit the [Learn](/learn) section.
98
</Intro>
109

11-
---
12-
13-
## State Hooks {/*state-hooks*/}
14-
15-
*State* lets a component ["remember" information like user input.](/learn/state-a-components-memory) For example, a form component can use state to store the input value, while an image gallery component can use state to store the selected image index.
16-
17-
To add state to a component, use one of these Hooks:
18-
19-
* [`useState`](/reference/react/useState) declares a state variable that you can update directly.
20-
* [`useReducer`](/reference/react/useReducer) declares a state variable with the update logic inside a [reducer function.](/learn/extracting-state-logic-into-a-reducer)
21-
22-
```js
23-
function ImageGallery() {
24-
const [index, setIndex] = useState(0);
25-
// ...
26-
```
27-
28-
---
29-
30-
## Context Hooks {/*context-hooks*/}
31-
32-
*Context* lets a component [receive information from distant parents without passing it as props.](/learn/passing-props-to-a-component) For example, your app's top-level component can pass the current UI theme to all components below, no matter how deep.
33-
34-
* [`useContext`](/reference/react/useContext) reads and subscribes to a context.
35-
36-
```js
37-
function Button() {
38-
const theme = useContext(ThemeContext);
39-
// ...
40-
```
41-
42-
---
43-
44-
## Ref Hooks {/*ref-hooks*/}
45-
46-
*Refs* let a component [hold some information that isn't used for rendering,](/learn/referencing-values-with-refs) like a DOM node or a timeout ID. Unlike with state, updating a ref does not re-render your component. Refs are an "escape hatch" from the React paradigm. They are useful when you need to work with non-React systems, such as the built-in browser APIs.
47-
48-
* [`useRef`](/reference/react/useRef) declares a ref. You can hold any value in it, but most often it's used to hold a DOM node.
49-
* [`useImperativeHandle`](/reference/react/useImperativeHandle) lets you customize the ref exposed by your component. This is rarely used.
50-
51-
```js
52-
function Form() {
53-
const inputRef = useRef(null);
54-
// ...
55-
```
56-
57-
---
58-
59-
## Effect Hooks {/*effect-hooks*/}
60-
61-
*Effects* let a component [connect to and synchronize with external systems.](/learn/synchronizing-with-effects) This includes dealing with network, browser DOM, animations, widgets written using a different UI library, and other non-React code.
62-
63-
* [`useEffect`](/reference/react/useEffect) connects a component to an external system.
64-
65-
```js
66-
function ChatRoom({ roomId }) {
67-
useEffect(() => {
68-
const connection = createConnection(roomId);
69-
connection.connect();
70-
return () => connection.disconnect();
71-
}, [roomId]);
72-
// ...
73-
```
10+
Our The React reference documentation is broken down into functional subsections:
7411

75-
Effects are an "escape hatch" from the React paradigm. Don't use Effects to orchestrate the data flow of your application. If you're not interacting with an external system, [you might not need an Effect.](/learn/you-might-not-need-an-effect)
12+
## React {/*react*/}
13+
Programmatic React features:
14+
* [Hooks](/reference/react/hooks) - Use different React features from your components.
15+
* [Components](/reference/react/components) - Documents built-in components that you can use in your JSX.
16+
* [APIs](/reference/react/apis) - APIs that are useful for defining components.
17+
* [Directives](/reference/react/directives) - Provide instructions to bundlers compatible with React Server Components.
7618

77-
There are two rarely used variations of `useEffect` with differences in timing:
78-
79-
* [`useLayoutEffect`](/reference/react/useLayoutEffect) fires before the browser repaints the screen. You can measure layout here.
80-
* [`useInsertionEffect`](/reference/react/useInsertionEffect) fires before React makes changes to the DOM. Libraries can insert dynamic CSS here.
81-
82-
---
83-
84-
## Performance Hooks {/*performance-hooks*/}
85-
86-
A common way to optimize re-rendering performance is to skip unnecessary work. For example, you can tell React to reuse a cached calculation or to skip a re-render if the data has not changed since the previous render.
87-
88-
To skip calculations and unnecessary re-rendering, use one of these Hooks:
89-
90-
- [`useMemo`](/reference/react/useMemo) lets you cache the result of an expensive calculation.
91-
- [`useCallback`](/reference/react/useCallback) lets you cache a function definition before passing it down to an optimized component.
92-
93-
```js
94-
function TodoList({ todos, tab, theme }) {
95-
const visibleTodos = useMemo(() => filterTodos(todos, tab), [todos, tab]);
96-
// ...
97-
}
98-
```
99-
100-
Sometimes, you can't skip re-rendering because the screen actually needs to update. In that case, you can improve performance by separating blocking updates that must be synchronous (like typing into an input) from non-blocking updates which don't need to block the user interface (like updating a chart).
101-
102-
To prioritize rendering, use one of these Hooks:
103-
104-
- [`useTransition`](/reference/react/useTransition) lets you mark a state transition as non-blocking and allow other updates to interrupt it.
105-
- [`useDeferredValue`](/reference/react/useDeferredValue) lets you defer updating a non-critical part of the UI and let other parts update first.
106-
107-
---
108-
109-
## Resource Hooks {/*resource-hooks*/}
110-
111-
*Resources* can be accessed by a component without having them as part of their state. For example, a component can read a message from a Promise or read styling information from a context.
112-
113-
To read a value from a resource, use this Hook:
114-
115-
- [`use`](/reference/react/use) lets you read the value of a resource like a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
116-
117-
```js
118-
function MessageComponent({ messagePromise }) {
119-
const message = use(messagePromise);
120-
const theme = use(ThemeContext);
121-
// ...
122-
}
123-
```
124-
125-
---
126-
127-
## Other Hooks {/*other-hooks*/}
128-
129-
These Hooks are mostly useful to library authors and aren't commonly used in the application code.
130-
131-
- [`useDebugValue`](/reference/react/useDebugValue) lets you customize the label React DevTools displays for your custom Hook.
132-
- [`useId`](/reference/react/useId) lets a component associate a unique ID with itself. Typically used with accessibility APIs.
133-
- [`useSyncExternalStore`](/reference/react/useSyncExternalStore) lets a component subscribe to an external store.
134-
135-
---
19+
## React DOM {/*react-dom*/}
20+
React-dom contains features that are only supported for web applications
21+
(which run in the browser DOM environment). This section is broken into the following:
13622

137-
## Your own Hooks {/*your-own-hooks*/}
23+
* [Hooks](/reference/react-dom/hooks) - Hooks for web applications which run in the browser DOM environment.
24+
* [Components](/reference/react-dom/components) - React supports all of the browser built-in HTML and SVG components.
25+
* [APIs](/reference/react-dom) - The `react-dom` package contains methods supported only in web applications.
26+
* [Client APIs](/reference/react-dom/client) - The `react-dom/client` APIs let you render React components on the client (in the browser).
27+
* [Server APIs](/reference/react-dom/server) - The `react-dom/server` APIs let you render React components to HTML on the server.
13828

139-
You can also [define your own custom Hooks](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component) as JavaScript functions.
29+
## Legacy APIs {/*legacy-apis*/}
30+
* [Legacy APIs](/reference/react/legacy) - Exported from the react package, but not recommended for use in newly written code.

0 commit comments

Comments
 (0)