-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Translate "useRef" reference #613
Conversation
Size changes📦 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! 🙌 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
細かいものだけですが、対応をお願いします。
@@ -31,34 +31,34 @@ function MyComponent() { | |||
// ... | |||
``` | |||
|
|||
[See more examples below.](#usage) | |||
[使用法をもっと見る](#usage) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[使用法をもっと見る](#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 の一部など)を保持している場合は、変更すべきではありません。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* state と違い、`ref.current` プロパティは変更することができます(ミュータブル)。ただし、レンダーに利用されるオブジェクト(state の一部など)を保持している場合は、変更すべきではありません。 | |
* `ref.current` プロパティは書き換えが可能です。つまり state と違いミュータブル (mutable) です。ただし、レンダーに利用されるオブジェクト(state の一部など)を保持している場合は、変更すべきではありません。 |
原文に近づけました。
(原文の後半は、どういうパターンを想定した話なんですかね…そもそもそんなデータは ref に入れるなよと冒頭でも 2 行後でも言ってるのに…)
@@ -68,11 +68,11 @@ function Stopwatch() { | |||
// ... | |||
``` | |||
|
|||
`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>を返します。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`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" がそもそも冗長なので消しました
|
||
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) と似ていますが、大きく違う点があります。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
次回以降のレンダーでも、`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>を手動で変更します。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
それは、**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 の一部というか。
|
||
### 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*/} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### 独自コンポーネントへの参照 (ref) を取得できない {/*i-cant-get-a-ref-to-a-custom-component*/} | |
### 独自コンポーネントへの ref を取得できない {/*i-cant-get-a-ref-to-a-custom-component*/} |
|
||
To fix this, find the component that you want to get a ref to: | ||
これを修正するには、まず、参照を取得したいコンポーネントを探します。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これを修正するには、まず、参照を取得したいコンポーネントを探します。 | |
これを修正するには、まず、ref を取得したいコンポーネントを探します。 |
|
||
<ConsoleBlock level="error"> | ||
|
||
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? | ||
|
||
</ConsoleBlock> | ||
|
||
By default, your own components don't expose refs to the DOM nodes inside them. | ||
デフォルトでは、独自コンポーネントは、内部の DOM ノードへの参照を公開していません。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
デフォルトでは、独自コンポーネントは、内部の DOM ノードへの参照を公開していません。 | |
デフォルトでは、独自コンポーネントは、内部の DOM ノードへの ref を公開していません。 |
@@ -591,6 +591,6 @@ const MyInput = forwardRef(({ value, onChange }, ref) => { | |||
export default MyInput; | |||
``` | |||
|
|||
Then the parent component can get a ref to it. | |||
これで、親コンポーネントから参照を取得できるようになります。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これで、親コンポーネントから参照を取得できるようになります。 | |
これで、親コンポーネントから ref を取得できるようになります。 |
|
||
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 を呼び出して、スクロールしたい画像を探しています。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この例では、ボタンがクリックされると、その画像が画面に表示されるようにスクロールします。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 を呼び出して、スクロール先の画像を探しています。 |
レビューありがとうございます!修正 done です。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 ありがとうございます!
リファレンスの "useRef" の翻訳です