Skip to content

Commit 8e4f0ea

Browse files
merging all conflicts
2 parents 3bc684c + 046f17d commit 8e4f0ea

3 files changed

Lines changed: 202 additions & 86 deletions

File tree

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

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,15 @@ Per visualizzare un'immagine, renderizza il [componente browser integrato `<img>
5656

5757
#### Caveats {/*caveats*/}
5858

59+
<<<<<<< HEAD
5960
* Non passare una stringa vuota a `src`: potrebbe indurre il browser a richiedere di nuovo la pagina corrente. React mostra un avviso in sviluppo e omette l'attributo. Per non visualizzare alcuna immagine, ometti `<img>` o passa `null` a `src`.
6061
* `<img>` non può avere children né usare `dangerouslySetInnerHTML`. React solleva un errore se passi una delle due cose.
6162
* `fetchPriority="low"` non impedisce a React di attendere che l'immagine venga caricata e decodificata durante un aggiornamento View Transition renderizzato lato client. Usa `loading="lazy"` o un gestore `onLoad` per rinunciare a questo comportamento.
63+
=======
64+
* 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`.
65+
* `<img>` cannot have children or use `dangerouslySetInnerHTML`. React throws an error if you pass either.
66+
* `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.
67+
>>>>>>> 046f17d04295ba047bb5739026b4ac3110f17028
6268
6369
---
6470

@@ -132,25 +138,36 @@ Per creare un hint di preload esplicito, chiama [`preload`](/reference/react-dom
132138

133139
### Attendere un'immagine durante una View Transition {/*waiting-for-an-image-during-a-view-transition*/}
134140

141+
<<<<<<< HEAD
135142
Durante un aggiornamento [`<ViewTransition>`](/reference/react/ViewTransition) renderizzato lato client, React potrebbe attendere che un'immagine si carichi e venga decodificata prima di avviare l'animazione. Questo si applica quando viene renderizzato un nuovo `<img>` con un `src` non vuoto, o quando `src` o `srcSet` di un'immagine esistente cambia. L'immagine deve trovarsi nel sottoalbero del `<ViewTransition>` e non deve avere `loading="lazy"` né un gestore `onLoad`. React non attende le immagini durante gli aggiornamenti sincroni.
136143

137144
Quando un boundary Suspense rivela contenuto in streaming all'interno di un `<ViewTransition>`, React potrebbe anche attendere le immagini visibili con un `src` non vuoto che non hanno `loading="lazy"`. React smette di attendere dopo un timeout, così che un'immagine lenta non blocchi l'aggiornamento all'infinito.
138145

139146
In questo esempio, il boundary Suspense è avvolto in un `<ViewTransition>` e mostra un placeholder del profilo finché il ritratto non si è caricato.
140147

141148
Per confronto, il secondo pulsante inserisce la stessa card direttamente nel DOM. La card appare immediatamente, e il browser visualizza l'immagine dopo che si è caricata:
149+
=======
150+
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.
151+
152+
Compare how the same image appears when a Suspense boundary reveals its content inside and outside a `<ViewTransition>`:
153+
>>>>>>> 046f17d04295ba047bb5739026b4ac3110f17028
142154
143155
<Sandpack>
144156

145157
```js
146-
import { ViewTransition, Suspense, useState, startTransition } from 'react';
147-
import { freshImageUrl } from './image.js';
148-
import VanillaProfile from './VanillaProfile.js';
149-
150-
function Profile({ src }) {
158+
import {
159+
ViewTransition,
160+
Suspense,
161+
use,
162+
useState,
163+
} from 'react';
164+
import { fetchImageSrc } from './image.js';
165+
166+
function Profile({ cacheKey }) {
167+
const src = use(fetchImageSrc(cacheKey));
151168
return (
152169
<div className="card">
153-
<img src={src} alt="Jack Pope" width={80} height={80} />
170+
<img src={src} alt="" width={80} height={80} />
154171
<p>Jack Pope</p>
155172
</div>
156173
);
@@ -166,25 +183,34 @@ function ProfilePlaceholder() {
166183
}
167184

168185
export default function App() {
169-
const [src, setSrc] = useState(null);
186+
const [showWithTransition, setShowWithTransition] =
187+
useState(false);
188+
const [showWithoutTransition, setShowWithoutTransition] =
189+
useState(false);
170190
return (
171191
<>
192+
<<<<<<< HEAD
172193
<button
173194
onClick={() => {
174195
startTransition(() => {
175196
setSrc(freshImageUrl());
176197
});
177198
}}>
178199
Mostra profilo
200+
=======
201+
<button onClick={() => setShowWithTransition(true)}>
202+
Show profile with View Transition
203+
>>>>>>> 046f17d04295ba047bb5739026b4ac3110f17028
179204
</button>
180-
{src && (
205+
{showWithTransition && (
181206
<ViewTransition>
182207
<Suspense fallback={<ProfilePlaceholder />}>
183-
<Profile src={src} />
208+
<Profile cacheKey="with-transition" />
184209
</Suspense>
185210
</ViewTransition>
186211
)}
187212
<hr />
213+
<<<<<<< HEAD
188214
<VanillaProfile />
189215
</>
190216
);
@@ -207,15 +233,42 @@ export default function VanillaProfile() {
207233
<>
208234
<button onClick={show}>Mostra profilo (aggiornamento diretto del DOM)</button>
209235
<div ref={ref} />
236+
=======
237+
<button onClick={() => setShowWithoutTransition(true)}>
238+
Show profile without View Transition
239+
</button>
240+
{showWithoutTransition && (
241+
<Suspense fallback={<ProfilePlaceholder />}>
242+
<Profile cacheKey="without-transition" />
243+
</Suspense>
244+
)}
245+
>>>>>>> 046f17d04295ba047bb5739026b4ac3110f17028
210246
</>
211247
);
212248
}
213249
```
214250

215251
```js src/image.js hidden
252+
<<<<<<< HEAD
216253
// Aggiungi un parametro univoco così l'immagine non finisce in cache
217254
// e ogni esecuzione mostra lo stato di caricamento.
218255
export function freshImageUrl() {
256+
=======
257+
// Normally, the caching logic would be inside a framework.
258+
const cache = new Map();
259+
260+
export function fetchImageSrc(cacheKey) {
261+
if (!cache.has(cacheKey)) {
262+
cache.set(cacheKey, loadImageSrc());
263+
}
264+
return cache.get(cacheKey);
265+
}
266+
267+
async function loadImageSrc() {
268+
// Delay the response so the Suspense fallback is visible.
269+
await new Promise(resolve => setTimeout(resolve, 1000));
270+
// Add a unique parameter so the image isn't cached.
271+
>>>>>>> 046f17d04295ba047bb5739026b4ac3110f17028
219272
return 'https://react.dev/images/team/jack-pope.jpg?t=' + Date.now();
220273
}
221274
```
@@ -262,3 +315,5 @@ hr {
262315
```
263316
264317
</Sandpack>
318+
319+
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.

0 commit comments

Comments
 (0)