Skip to content

Commit d731352

Browse files
Merge branch 'main' of https://github.com/reactjs/react.dev into sync-0821e7d9
2 parents e9b8865 + 0821e7d commit d731352

11 files changed

Lines changed: 790 additions & 89 deletions

File tree

‎src/content/reference/dev-tools/react-devtools-cdt-mcp.md‎

Lines changed: 407 additions & 0 deletions
Large diffs are not rendered by default.

‎src/content/reference/react-compiler/directives.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,4 @@ For specific issues with directives, see the troubleshooting sections in:
195195

196196
* [`compilationMode`](/reference/react-compiler/compilationMode) - Configure how the compiler chooses what to optimize
197197
* [`Configuration`](/reference/react-compiler/configuration) - Full compiler configuration options
198-
* [React Compiler documentation](https://react.dev/learn/react-compiler) - Getting started guide
198+
* [React Compiler documentation](/learn/react-compiler) - Getting started guide
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
---
2+
title: "<img>"
3+
---
4+
5+
<Intro>
6+
7+
The [built-in browser `<img>` component](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img) lets you embed an image.
8+
9+
```js
10+
<img src="photo.jpg" alt="A person walking through a park" />
11+
```
12+
13+
</Intro>
14+
15+
<InlineToc />
16+
17+
---
18+
19+
## Reference {/*reference*/}
20+
21+
### `<img>` {/*img*/}
22+
23+
To display an image, render the [built-in browser `<img>` component](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img).
24+
25+
```js
26+
<img src="photo.jpg" alt="A person walking through a park" />
27+
```
28+
29+
[See more examples below.](#usage)
30+
31+
#### Props {/*props*/}
32+
33+
`<img>` supports all [common element props.](/reference/react-dom/components/common#common-props)
34+
35+
* `alt`: a string. Specifies alternative text for the image. Use an empty string for a purely decorative image.
36+
* `crossOrigin`: a string. Specifies the [CORS policy](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) to use when fetching the image. The possible values are `anonymous` and `use-credentials`.
37+
* `decoding`: a string. Suggests whether the browser should wait to decode the image before presenting other content. The possible values are `async`, `sync`, and `auto` (the default).
38+
* `fetchPriority`: a string. Suggests a relative priority for fetching the image. The possible values are `high`, `low`, and `auto` (the default). During server rendering, `fetchPriority="low"` also prevents React from [automatically preloading the image.](#controlling-image-preloading-during-server-rendering)
39+
* `height`: a number or string. Specifies the rendered height of the image.
40+
* `loading`: a string. Specifies whether the browser should defer loading the image until it is near the viewport. The possible values are `eager` (the default) and `lazy`. Setting `loading="lazy"` prevents React from [automatically preloading the image.](#controlling-image-preloading-during-server-rendering)
41+
* `onError`: an [event handler](/reference/react-dom/components/common#event-handler) function. Fires when the image fails to load.
42+
* `onLoad`: an [event handler](/reference/react-dom/components/common#event-handler) function. Fires when the image finishes loading. Passing `onLoad` prevents React from [waiting for the image during a client-rendered View Transition update.](#waiting-for-an-image-during-a-view-transition)
43+
* `referrerPolicy`: a string. Specifies the [referrer information](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#referrerpolicy) to send when fetching the image.
44+
* `sizes`: a string. Specifies the image sizes for different page layouts. Used with `srcSet`.
45+
* `src`: a string. Specifies the URL of the image.
46+
* `srcSet`: a string. Specifies one or more candidate image sources for the browser to choose from.
47+
* `useMap`: a string. Associates the image with a [client-side image map](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/map).
48+
* `width`: a number or string. Specifies the rendered width of the image.
49+
50+
#### Caveats {/*caveats*/}
51+
52+
* Do not pass an empty string to `src`. It may cause the browser to request the current page again. React warns in development and omits the attribute. To render no image, omit the `<img>` or pass `null` to `src`.
53+
* `<img>` cannot have children or use `dangerouslySetInnerHTML`. React throws an error if you pass either.
54+
* `fetchPriority="low"` does not stop React from waiting for the image to load during a client-rendered View Transition update. Use `loading="lazy"` or an `onLoad` handler to opt out of that behavior.
55+
56+
---
57+
58+
## Usage {/*usage*/}
59+
60+
### Displaying an image {/*displaying-an-image*/}
61+
62+
Pass the image URL to `src` and a text description to `alt`:
63+
64+
<Sandpack>
65+
66+
```js
67+
export default function Profile() {
68+
return (
69+
<img
70+
src="https://react.dev/images/docs/scientists/yXOvdOSs.jpg"
71+
alt="Hedy Lamarr"
72+
width={100}
73+
height={100}
74+
/>
75+
);
76+
}
77+
```
78+
79+
```css
80+
img {
81+
border-radius: 50%;
82+
object-fit: cover;
83+
}
84+
```
85+
86+
</Sandpack>
87+
88+
Specify `width` and `height` when you know the image dimensions so the browser can reserve space before the image loads. For a decorative image, pass `alt=""` so that screen readers ignore it.
89+
90+
---
91+
92+
### Controlling image preloading during server rendering {/*controlling-image-preloading-during-server-rendering*/}
93+
94+
During server rendering, React automatically generates a preload hint for an `<img>` by default. This can let the browser start fetching the image before it encounters the `<img>` in the rendered HTML.
95+
96+
Add `loading="lazy"` or `fetchPriority="low"` to an image that should not receive this hint:
97+
98+
```js
99+
function ProductPage() {
100+
return (
101+
<>
102+
<img src="hero.jpg" alt="Featured product" />
103+
<img src="thumbnail.jpg" alt="Related product" loading="lazy" />
104+
<img src="secondary.jpg" alt="Another product" fetchPriority="low" />
105+
</>
106+
);
107+
}
108+
```
109+
110+
In this example, React generates a preload hint only for `hero.jpg`. Depending on the server API or framework, React may render the equivalent of this element:
111+
112+
```html
113+
<link rel="preload" as="image" href="hero.jpg" />
114+
```
115+
116+
React may instead provide the same hint in a `Link` response header. The other two images keep their `loading` and `fetchPriority` props in the rendered HTML, but React does not generate preload hints for them. The `loading="lazy"` prop asks the browser to defer loading an image until it approaches the viewport. The `fetchPriority="low"` prop allows the image to load immediately, but tells the browser to fetch it at a lower priority.
117+
118+
React also does not automatically preload an image when it is inside a `<picture>` or `<noscript>` element, or when its `src` or `srcSet` is a data URL.
119+
120+
If you render an image through a framework or a component library, consult its documentation for the default behavior. React decides whether to generate an automatic preload from the props of the underlying `<img>`. For example, an image component may add `loading="lazy"` by default and provide a separate option for explicitly preloading selected images.
121+
122+
To create an explicit preload hint, call [`preload`](/reference/react-dom/preload).
123+
124+
---
125+
126+
### Waiting for an image during a View Transition {/*waiting-for-an-image-during-a-view-transition*/}
127+
128+
During a [`<ViewTransition>`](/reference/react/ViewTransition) update, React waits up to 500 ms for visible images to load before starting the animation. This includes newly rendered `<img>` elements and existing images whose `src` or `srcSet` changes. Setting `loading="lazy"` or adding an `onLoad` handler opts an image out.
129+
130+
Compare how the same image appears when a Suspense boundary reveals its content inside and outside a `<ViewTransition>`:
131+
132+
<Sandpack>
133+
134+
```js
135+
import {
136+
ViewTransition,
137+
Suspense,
138+
use,
139+
useState,
140+
} from 'react';
141+
import { fetchImageSrc } from './image.js';
142+
143+
function Profile({ cacheKey }) {
144+
const src = use(fetchImageSrc(cacheKey));
145+
return (
146+
<div className="card">
147+
<img src={src} alt="" width={80} height={80} />
148+
<p>Jack Pope</p>
149+
</div>
150+
);
151+
}
152+
153+
function ProfilePlaceholder() {
154+
return (
155+
<div className="card">
156+
<div className="avatar-placeholder" />
157+
<p className="name-placeholder">&nbsp;</p>
158+
</div>
159+
);
160+
}
161+
162+
export default function App() {
163+
const [showWithTransition, setShowWithTransition] =
164+
useState(false);
165+
const [showWithoutTransition, setShowWithoutTransition] =
166+
useState(false);
167+
return (
168+
<>
169+
<button onClick={() => setShowWithTransition(true)}>
170+
Show profile with View Transition
171+
</button>
172+
{showWithTransition && (
173+
<ViewTransition>
174+
<Suspense fallback={<ProfilePlaceholder />}>
175+
<Profile cacheKey="with-transition" />
176+
</Suspense>
177+
</ViewTransition>
178+
)}
179+
<hr />
180+
<button onClick={() => setShowWithoutTransition(true)}>
181+
Show profile without View Transition
182+
</button>
183+
{showWithoutTransition && (
184+
<Suspense fallback={<ProfilePlaceholder />}>
185+
<Profile cacheKey="without-transition" />
186+
</Suspense>
187+
)}
188+
</>
189+
);
190+
}
191+
```
192+
193+
```js src/image.js hidden
194+
// Normally, the caching logic would be inside a framework.
195+
const cache = new Map();
196+
197+
export function fetchImageSrc(cacheKey) {
198+
if (!cache.has(cacheKey)) {
199+
cache.set(cacheKey, loadImageSrc());
200+
}
201+
return cache.get(cacheKey);
202+
}
203+
204+
async function loadImageSrc() {
205+
// Delay the response so the Suspense fallback is visible.
206+
await new Promise(resolve => setTimeout(resolve, 1000));
207+
// Add a unique parameter so the image isn't cached.
208+
return 'https://react.dev/images/team/jack-pope.jpg?t=' + Date.now();
209+
}
210+
```
211+
212+
```css
213+
#root {
214+
min-height: 390px;
215+
}
216+
.card {
217+
margin-top: 1em;
218+
}
219+
.card img {
220+
display: block;
221+
border-radius: 50%;
222+
background: #dfe3e9;
223+
}
224+
.card p {
225+
font-weight: bold;
226+
}
227+
.avatar-placeholder {
228+
width: 80px;
229+
height: 80px;
230+
border-radius: 50%;
231+
background: #dfe3e9;
232+
}
233+
.name-placeholder {
234+
width: 90px;
235+
border-radius: 4px;
236+
background: #dfe3e9;
237+
}
238+
hr {
239+
margin: 16px 0;
240+
}
241+
```
242+
243+
```json package.json hidden
244+
{
245+
"dependencies": {
246+
"react": "19.3.0",
247+
"react-dom": "19.3.0",
248+
"react-scripts": "latest"
249+
}
250+
}
251+
```
252+
253+
</Sandpack>
254+
255+
With `<ViewTransition>`, React keeps the skeleton visible for up to 500 ms while the image loads, so the card can be revealed with its image already in place. Without `<ViewTransition>`, Suspense stops showing the skeleton as soon as the Promise resolves. If the image is still loading, the card appears first and the image pops in afterward.

‎src/content/reference/react-dom/components/index.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ They are special in React because passing the `value` prop to them makes them *[
3636

3737
These built-in browser components let you load external resources or annotate the document with metadata:
3838

39+
* [`<img>`](/reference/react-dom/components/img)
3940
* [`<link>`](/reference/react-dom/components/link)
4041
* [`<meta>`](/reference/react-dom/components/meta)
4142
* [`<script>`](/reference/react-dom/components/script)
@@ -91,7 +92,7 @@ React supports all built-in browser HTML components. This includes:
9192
* [`<html>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html)
9293
* [`<i>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/i)
9394
* [`<iframe>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe)
94-
* [`<img>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img)
95+
* [`<img>`](/reference/react-dom/components/img)
9596
* [`<input>`](/reference/react-dom/components/input)
9697
* [`<ins>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ins)
9798
* [`<kbd>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/kbd)

‎src/content/reference/react-dom/preinitModule.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The `preinitModule` function provides the browser with a hint that it should sta
5050
* `crossOrigin`: a string. The [CORS policy](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) to use. Its possible values are `anonymous` and `use-credentials`.
5151
* `integrity`: a string. A cryptographic hash of the module, to [verify its authenticity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity).
5252
* `nonce`: a string. A cryptographic [nonce to allow the module](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce) when using a strict Content Security Policy.
53+
* `fetchPriority`: a string. Suggests a [relative priority](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/fetchpriority) for fetching the resource; possible values are `auto` (default), `high`, and `low`.
5354

5455
#### Returns {/*returns*/}
5556

‎src/content/reference/react-dom/preloadModule.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The `preloadModule` function provides the browser with a hint that it should sta
5050
* `crossOrigin`: a string. The [CORS policy](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) to use. Its possible values are `anonymous` and `use-credentials`.
5151
* `integrity`: a string. A cryptographic hash of the module, to [verify its authenticity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity).
5252
* `nonce`: a string. A cryptographic [nonce to allow the module](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce) when using a strict Content Security Policy.
53+
* `fetchPriority`: a string. Suggests a [relative priority](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/fetchpriority) for fetching the resource; possible values are `auto` (default), `high`, and `low`.
5354

5455

5556
#### Returns {/*returns*/}

0 commit comments

Comments
 (0)