Skip to content

Commit 44bd0c3

Browse files
committed
Translate "<img>"
1 parent 356741e commit 44bd0c3

1 file changed

Lines changed: 79 additions & 81 deletions

File tree

  • src/content/reference/react-dom/components

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

Lines changed: 79 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: "<img>"
44

55
<Intro>
66

7-
The [built-in browser `<img>` component](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img) lets you embed an image.
7+
[ブラウザ組み込みの `<img>` コンポーネント](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img)を使うことで、画像を埋め込むことができます。
88

99
```js
1010
<img src="photo.jpg" alt="A person walking through a park" />
@@ -16,50 +16,50 @@ The [built-in browser `<img>` component](https://developer.mozilla.org/en-US/doc
1616

1717
---
1818

19-
## Reference {/*reference*/}
19+
## リファレンス {/*reference*/}
2020

2121
### `<img>` {/*img*/}
2222

23-
To display an image, render the [built-in browser `<img>` component](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img).
23+
画像を表示するには、[ブラウザ組み込みの `<img>` コンポーネント](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img)をレンダーします。
2424

2525
```js
2626
<img src="photo.jpg" alt="A person walking through a park" />
2727
```
2828

29-
[See more examples below.](#usage)
29+
[さらに例を見る](#usage)
3030

31-
#### Props {/*props*/}
31+
#### props {/*props*/}
3232

33-
`<img>` supports all [common element props.](/reference/react-dom/components/common#common-props)
33+
`<img>` は、[一般的な要素の props](/reference/react-dom/components/common#common-props) をすべてサポートしています。
3434

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.
35+
* `alt`: 文字列。画像の代替テキストを指定します。純粋に装飾目的の画像には空文字列を使用します。
36+
* `crossOrigin`: 文字列。画像のフェッチ時に使用する [CORS ポリシー](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin)を指定します。可能な値は `anonymous` と `use-credentials` です。
37+
* `decoding`: 文字列。他のコンテンツを表示する前に、ブラウザが画像のデコードを待つべきかどうかのヒントです。可能な値は `async`、`sync`、`auto`(デフォルト)です。
38+
* `fetchPriority`: 文字列。画像のフェッチに対する相対的な優先度のヒントです。可能な値は `high`、`low`、`auto`(デフォルト)です。サーバレンダリング中は、`fetchPriority="low"` を指定すると、React による[画像の自動プリロード](#controlling-image-preloading-during-server-rendering)も行われなくなります。
39+
* `height`: 数値または文字列。画像の表示時の高さを指定します。
40+
* `loading`: 文字列。画像がビューポートの近くに来るまで、ブラウザが画像の読み込みを遅延させるかどうかを指定します。可能な値は `eager`(デフォルト)と `lazy` です。`loading="lazy"` を設定すると、React による[画像の自動プリロード](#controlling-image-preloading-during-server-rendering)が行われなくなります。
41+
* `onError`: [イベントハンドラ](/reference/react-dom/components/common#event-handler)関数。画像の読み込みに失敗したときに発火します。
42+
* `onLoad`: [イベントハンドラ](/reference/react-dom/components/common#event-handler)関数。画像の読み込みが完了したときに発火します。`onLoad` を渡すと、[クライアントでのレンダーによるビュー遷移 (View Transition) の更新中に、React が画像を待機する動作](#waiting-for-an-image-during-a-view-transition)が行われなくなります。
43+
* `referrerPolicy`: 文字列。画像のフェッチ時に送信する[リファラ情報](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#referrerpolicy)を指定します。
44+
* `sizes`: 文字列。ページレイアウトごとの画像サイズを指定します。`srcSet` と組み合わせて使用します。
45+
* `src`: 文字列。画像の URL を指定します。
46+
* `srcSet`: 文字列。ブラウザが選択できる画像ソースの候補を 1 つ以上指定します。
47+
* `useMap`: 文字列。画像を[クライアントサイドイメージマップ](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/map)と関連付けます。
48+
* `width`: 数値または文字列。画像の表示時の幅を指定します。
4949

50-
#### Caveats {/*caveats*/}
50+
#### 注意点 {/*caveats*/}
5151

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 and decode during a client-rendered View Transition update. Use `loading="lazy"` or an `onLoad` handler to opt out of that behavior.
52+
* `src` に空文字列を渡さないでください。ブラウザが現在のページを再度リクエストしてしまう可能性があります。React は開発環境では警告を表示し、この属性を省略します。画像を何も表示しない場合は、`<img>` 自体を省略するか、`src` に `null` を渡してください。
53+
* `<img>` は子要素を持つことができず、`dangerouslySetInnerHTML` も使用できません。いずれかを渡すと React はエラーをスローします。
54+
* `fetchPriority="low"` を指定しても、クライアントでのレンダーによるビュー遷移の更新中に React が画像の読み込みを待機する動作は止まりません。この動作をオプトアウトするには、`loading="lazy"` を指定するか `onLoad` ハンドラを渡してください。
5555

5656
---
5757

58-
## Usage {/*usage*/}
58+
## 使用法 {/*usage*/}
5959

60-
### Displaying an image {/*displaying-an-image*/}
60+
### 画像を表示する {/*displaying-an-image*/}
6161

62-
Pass the image URL to `src` and a text description to `alt`:
62+
`src` に画像の URL を、`alt` にテキストによる説明を渡します。
6363

6464
<Sandpack>
6565

@@ -85,15 +85,15 @@ img {
8585

8686
</Sandpack>
8787

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.
88+
画像のサイズが分かっている場合は `width` と `height` を指定してください。これにより、ブラウザが画像の読み込み前に表示領域を確保できるようになります。装飾目的の画像には `alt=""` を渡して、スクリーンリーダに無視されるようにしてください。
8989

9090
---
9191

92-
### Controlling image preloading during server rendering {/*controlling-image-preloading-during-server-rendering*/}
92+
### サーバレンダリング中の画像のプリロードを制御する {/*controlling-image-preloading-during-server-rendering*/}
9393

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.
94+
サーバレンダリング中、React はデフォルトで `<img>` に対するプリロードヒントを自動的に生成します。これにより、ブラウザはレンダーされた HTML 内で `<img>` に出会う前に、画像のフェッチを開始できるようになります。
9595

96-
Add `loading="lazy"` or `fetchPriority="low"` to an image that should not receive this hint:
96+
このヒントを生成すべきでない画像には、`loading="lazy"` または `fetchPriority="low"` を追加します。
9797

9898
```js
9999
function ProductPage() {
@@ -107,43 +107,44 @@ function ProductPage() {
107107
}
108108
```
109109

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:
110+
この例では、React は `hero.jpg` に対してのみプリロードヒントを生成します。使用するサーバ API やフレームワークによっては、React は以下のような要素に相当するものをレンダーすることがあります。
111111

112112
```html
113113
<link rel="preload" as="image" href="hero.jpg" />
114114
```
115115

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.
116+
代わりに、同じヒントを `Link` レスポンスヘッダで提供する場合もあります。残りの 2 つの画像については、レンダーされた HTML に `loading` や `fetchPriority` の props がそのまま残りますが、React はプリロードヒントを生成しません。`loading="lazy"` は、画像がビューポートに近づくまで読み込みを遅延させるようブラウザに求めます。`fetchPriority="low"` は画像をすぐに読み込むことを許可しますが、低い優先度でフェッチするようブラウザに伝えます。
117117

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.
118+
また、画像が `<picture>` 要素や `<noscript>` 要素の内部にある場合や、`src` または `srcSet` が data URL である場合も、React は画像を自動的にプリロードしません。
119119

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.
120+
フレームワークやコンポーネントライブラリを通じて画像をレンダーしている場合は、デフォルトの動作についてそれぞれのドキュメントを参照してください。自動プリロードを生成するかどうかは、内部でレンダーされる `<img>` の props に基づいて React が決定します。例えば、画像コンポーネントによっては、デフォルトで `loading="lazy"` を付加し、選択した画像を明示的にプリロードするための別のオプションを提供している場合があります。
121121

122-
To create an explicit preload hint, call [`preload`](/reference/react-dom/preload).
122+
プリロードヒントを明示的に作成するには、[`preload`](/reference/react-dom/preload) を呼び出します。
123123

124124
---
125125

126-
### Waiting for an image during a View Transition {/*waiting-for-an-image-during-a-view-transition*/}
126+
### ビュー遷移中に画像の読み込みを待機する {/*waiting-for-an-image-during-a-view-transition*/}
127127

128-
During a client-rendered [`<ViewTransition>`](/reference/react/ViewTransition) update, React may wait for an image to load and decode before starting the animation. This applies when a new `<img>` with a non-empty `src` is rendered, or when an existing image's `src` or `srcSet` changes. The image must be inside the `<ViewTransition>` subtree and must not have `loading="lazy"` or an `onLoad` handler. React does not wait for images during synchronous updates.
128+
[`<ViewTransition>`](/reference/react/ViewTransition) による更新中、React はアニメーションを開始する前に、表示される画像の読み込みを最大 500 ミリ秒待機します。この対象には、新たにレンダーされた `<img>` 要素と、`src` や `srcSet` が変更された既存の画像が含まれます。`loading="lazy"` を設定するか `onLoad` ハンドラを追加すると、その画像は対象外になります。
129129

130-
When a Suspense boundary reveals streamed content inside a `<ViewTransition>`, React may also wait for visible images with a non-empty `src` that do not have `loading="lazy"`. React stops waiting after a timeout so that a slow image does not block the update indefinitely.
131-
132-
In this example, the Suspense boundary is wrapped in a `<ViewTransition>` and shows a profile skeleton until the portrait has loaded.
133-
134-
For comparison, the second button inserts the same card directly into the DOM. The card appears immediately, and the browser displays the image after it loads:
130+
サスペンスバウンダリが中身を表示する際に、`<ViewTransition>` の内側と外側とで同じ画像の表示のされ方がどう異なるか、比較してみましょう。
135131

136132
<Sandpack>
137133

138134
```js
139-
import { ViewTransition, Suspense, useState, startTransition } from 'react';
140-
import { freshImageUrl } from './image.js';
141-
import VanillaProfile from './VanillaProfile.js';
142-
143-
function Profile({ src }) {
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));
144145
return (
145146
<div className="card">
146-
<img src={src} alt="Jack Pope" width={80} height={80} />
147+
<img src={src} alt="" width={80} height={80} />
147148
<p>Jack Pope</p>
148149
</div>
149150
);
@@ -159,56 +160,51 @@ function ProfilePlaceholder() {
159160
}
160161

161162
export default function App() {
162-
const [src, setSrc] = useState(null);
163+
const [showWithTransition, setShowWithTransition] =
164+
useState(false);
165+
const [showWithoutTransition, setShowWithoutTransition] =
166+
useState(false);
163167
return (
164168
<>
165-
<button
166-
onClick={() => {
167-
startTransition(() => {
168-
setSrc(freshImageUrl());
169-
});
170-
}}>
171-
Show profile
169+
<button onClick={() => setShowWithTransition(true)}>
170+
Show profile with View Transition
172171
</button>
173-
{src && (
172+
{showWithTransition && (
174173
<ViewTransition>
175174
<Suspense fallback={<ProfilePlaceholder />}>
176-
<Profile src={src} />
175+
<Profile cacheKey="with-transition" />
177176
</Suspense>
178177
</ViewTransition>
179178
)}
180179
<hr />
181-
<VanillaProfile />
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+
)}
182188
</>
183189
);
184190
}
185191
```
186192

187-
```js src/VanillaProfile.js
188-
import { useRef } from 'react';
189-
import { freshImageUrl } from './image.js';
193+
```js src/image.js hidden
194+
// Normally, the caching logic would be inside a framework.
195+
const cache = new Map();
190196

191-
export default function VanillaProfile() {
192-
const ref = useRef(null);
193-
function show() {
194-
ref.current.innerHTML = `<div class="card">
195-
<img src="${freshImageUrl()}" alt="Jack Pope" width="80" height="80" />
196-
<p>Jack Pope</p>
197-
</div>`;
197+
export function fetchImageSrc(cacheKey) {
198+
if (!cache.has(cacheKey)) {
199+
cache.set(cacheKey, loadImageSrc());
198200
}
199-
return (
200-
<>
201-
<button onClick={show}>Show profile (direct DOM update)</button>
202-
<div ref={ref} />
203-
</>
204-
);
201+
return cache.get(cacheKey);
205202
}
206-
```
207203

208-
```js src/image.js hidden
209-
// Add a unique parameter so the image isn't cached,
210-
// and every run shows the loading state.
211-
export function freshImageUrl() {
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.
212208
return 'https://react.dev/images/team/jack-pope.jpg?t=' + Date.now();
213209
}
214210
```
@@ -255,3 +251,5 @@ hr {
255251
```
256252

257253
</Sandpack>
254+
255+
`<ViewTransition>` がある場合、React は画像の読み込み中、最大 500 ミリ秒間スケルトンを表示し続けるため、画像が揃った状態でカードを表示できます。`<ViewTransition>` がない場合、サスペンスはプロミスが解決した時点でスケルトンの表示をやめます。画像がまだ読み込み中であれば、先にカードが表示され、画像は後から表示されます。

0 commit comments

Comments
 (0)