Merged
Conversation
Size changesDetails📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |
yuta-ike
commented
Jun 25, 2023
smikitky
requested changes
Jun 29, 2023
| ``` | ||
|
|
||
| [See more examples below.](#usage) | ||
| [使用法をもっと見る](#usage) |
Member
There was a problem hiding this comment.
Suggested change
| [使用法をもっと見る](#usage) | |
| [さらに例を見る](#usage) |
| * When you change the `ref.current` property, React does not re-render your component. React is not aware of when you change it because a ref is a plain JavaScript object. | ||
| * Do not write _or read_ `ref.current` during rendering, except for [initialization.](#avoiding-recreating-the-ref-contents) This makes your component's behavior unpredictable. | ||
| * In Strict Mode, React will **call your component function twice** in order to [help you find accidental impurities.](#my-initializer-or-updater-function-runs-twice) This is development-only behavior and does not affect production. Each ref object will be created twice, but one of the versions will be discarded. If your component function is pure (as it should be), this should not affect the behavior. | ||
| * state と違い、`ref.current` プロパティは変更することができます(ミュータブル)。ただし、レンダーに利用されるオブジェクト(state の一部など)を保持している場合は、変更すべきではありません。 |
Member
There was a problem hiding this comment.
Suggested change
| * state と違い、`ref.current` プロパティは変更することができます(ミュータブル)。ただし、レンダーに利用されるオブジェクト(state の一部など)を保持している場合は、変更すべきではありません。 | |
| * `ref.current` プロパティは書き換えが可能です。つまり state と違いミュータブル (mutable) です。ただし、レンダーに利用されるオブジェクト(state の一部など)を保持している場合は、変更すべきではありません。 |
原文に近づけました。
(原文の後半は、どういうパターンを想定した話なんですかね…そもそもそんなデータは ref に入れるなよと冒頭でも 2 行後でも言ってるのに…)
| ``` | ||
|
|
||
| `useRef` returns a <CodeStep step={1}>ref object</CodeStep> with a single <CodeStep step={2}>`current` property</CodeStep> initially set to the <CodeStep step={3}>initial value</CodeStep> you provided. | ||
| `useRef` は、<CodeStep step={2}>`current` プロパティ</CodeStep>に、最初に指定した<CodeStep step={3}>初期値</CodeStep>が設定された状態の <CodeStep step={1}>ref オブジェクト</CodeStep>を返します。 |
Member
There was a problem hiding this comment.
Suggested change
| `useRef` は、<CodeStep step={2}>`current` プロパティ</CodeStep>に、最初に指定した<CodeStep step={3}>初期値</CodeStep>が設定された状態の <CodeStep step={1}>ref オブジェクト</CodeStep>を返します。 | |
| `useRef` は、唯一のプロパティである<CodeStep step={2}>`current`</CodeStep>に、指定された<CodeStep step={3}>初期値</CodeStep>が設定された状態の <CodeStep step={1}>ref オブジェクト</CodeStep>を返します。 |
- single を訳出してみました
- initially は逆にここに「最初に」と書くと混乱しそうですし、"initially set to the initial value" がそもそも冗長なので消しました
| `useRef` は、<CodeStep step={2}>`current` プロパティ</CodeStep>に、最初に指定した<CodeStep step={3}>初期値</CodeStep>が設定された状態の <CodeStep step={1}>ref オブジェクト</CodeStep>を返します。 | ||
|
|
||
| On the next renders, `useRef` will return the same object. You can change its `current` property to store information and read it later. This might remind you of [state](/reference/react/useState), but there is an important difference. | ||
| 次回以降のレンダーでも、`useRef` は同じオブジェクトを返します。これを利用して、データを保存するために `current` プロパティの値を変更し、あとからその値を読み出すことができます。これは [state](/reference/react/useState) と似ていますが、大きく違う点があります。 |
Member
There was a problem hiding this comment.
Suggested change
| 次回以降のレンダーでも、`useRef` は同じオブジェクトを返します。これを利用して、データを保存するために `current` プロパティの値を変更し、あとからその値を読み出すことができます。これは [state](/reference/react/useState) と似ていますが、大きく違う点があります。 | |
| 次回以降のレンダーでも、`useRef` は同じオブジェクトを返します。このオブジェクトの `current` プロパティを書き換えることで情報を保存しておき、あとからその値を読み出すことができます。これは [state](/reference/react/useState) と似ていますが、大きく違う点があります。 |
| 次回以降のレンダーでも、`useRef` は同じオブジェクトを返します。これを利用して、データを保存するために `current` プロパティの値を変更し、あとからその値を読み出すことができます。これは [state](/reference/react/useState) と似ていますが、大きく違う点があります。 | ||
|
|
||
| **Changing a ref does not trigger a re-render.** This means refs are perfect for storing information that doesn't affect the visual output of your component. For example, if you need to store an [interval ID](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) and retrieve it later, you can put it in a ref. To update the value inside the ref, you need to manually change its <CodeStep step={2}>`current` property</CodeStep>: | ||
| それは、**ref を変更しても、再レンダーはトリガされない**ことです。このことから、ref は、コンポーネントの UI に影響しないデータを保存するのに適しています。例えば、[interval ID](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) を保持しておき、あとから利用したい場合、ref に保存することができます。ref 内の値を更新するには、<CodeStep step={2}>`current` プロパティ</CodeStep>を手動で変更します。 |
Member
There was a problem hiding this comment.
Suggested change
| それは、**ref を変更しても、再レンダーはトリガされない**ことです。このことから、ref は、コンポーネントの UI に影響しないデータを保存するのに適しています。例えば、[interval ID](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) を保持しておき、あとから利用したい場合、ref に保存することができます。ref 内の値を更新するには、<CodeStep step={2}>`current` プロパティ</CodeStep>を手動で変更します。 | |
| それは、**ref を変更しても、再レンダーはトリガされない**ということです。このことから、ref は、出力されるコンポーネントの外見に影響しないデータを保存するのに適しています。例えば、[インターバルの ID](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) を保持しておき、あとから利用したい場合、ref に保存することができます。ref 内の値を更新するには、<CodeStep step={2}>`current` プロパティ</CodeStep>を手動で変更します。 |
本来は "UI" は "visual output" よりはもう少し包括的な概念のような気がします。エフェクトやイベントハンドラも UI の一部というか。
| ## トラブルシューティング {/*troubleshooting*/} | ||
|
|
||
| ### I can't get a ref to a custom component {/*i-cant-get-a-ref-to-a-custom-component*/} | ||
| ### 独自コンポーネントへの参照 (ref) を取得できない {/*i-cant-get-a-ref-to-a-custom-component*/} |
Member
There was a problem hiding this comment.
Suggested change
| ### 独自コンポーネントへの参照 (ref) を取得できない {/*i-cant-get-a-ref-to-a-custom-component*/} | |
| ### 独自コンポーネントへの ref を取得できない {/*i-cant-get-a-ref-to-a-custom-component*/} |
| デフォルトでは、独自コンポーネントは、内部の DOM ノードへの参照を公開していません。 | ||
|
|
||
| To fix this, find the component that you want to get a ref to: | ||
| これを修正するには、まず、参照を取得したいコンポーネントを探します。 |
Member
There was a problem hiding this comment.
Suggested change
| これを修正するには、まず、参照を取得したいコンポーネントを探します。 | |
| これを修正するには、まず、ref を取得したいコンポーネントを探します。 |
| </ConsoleBlock> | ||
|
|
||
| By default, your own components don't expose refs to the DOM nodes inside them. | ||
| デフォルトでは、独自コンポーネントは、内部の DOM ノードへの参照を公開していません。 |
Member
There was a problem hiding this comment.
Suggested change
| デフォルトでは、独自コンポーネントは、内部の DOM ノードへの参照を公開していません。 | |
| デフォルトでは、独自コンポーネントは、内部の DOM ノードへの ref を公開していません。 |
| ``` | ||
|
|
||
| Then the parent component can get a ref to it. | ||
| これで、親コンポーネントから参照を取得できるようになります。 |
Member
There was a problem hiding this comment.
Suggested change
| これで、親コンポーネントから参照を取得できるようになります。 | |
| これで、親コンポーネントから ref を取得できるようになります。 |
| #### スクロールして画像を表示 {/*scrolling-an-image-into-view*/} | ||
|
|
||
| In this example, clicking the button will scroll an image into view. It uses a ref to the list DOM node, and then calls DOM [`querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) API to find the image we want to scroll to. | ||
| この例では、ボタンがクリックされると、その画像が画面に表示されるようにスクロールします。ref を使用してリストの DOM ノードを取得し、DOM の [`querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) API を呼び出して、スクロールしたい画像を探しています。 |
Member
There was a problem hiding this comment.
Suggested change
| この例では、ボタンがクリックされると、その画像が画面に表示されるようにスクロールします。ref を使用してリストの DOM ノードを取得し、DOM の [`querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) API を呼び出して、スクロールしたい画像を探しています。 | |
| この例では、ボタンがクリックされると、その画像が画面に表示されるようにスクロールします。ref を使用してリストの DOM ノードを取得し、DOM の [`querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) API を呼び出して、スクロール先の画像を探しています。 |
Contributor
Author
|
レビューありがとうございます!修正 done です。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
リファレンスの "useRef" の翻訳です