-
Notifications
You must be signed in to change notification settings - Fork 177
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 useImperativeHandle reference #614
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,7 +4,7 @@ title: useImperativeHandle | |||||
|
||||||
<Intro> | ||||||
|
||||||
`useImperativeHandle` is a React Hook that lets you customize the handle exposed as a [ref.](/learn/manipulating-the-dom-with-refs) | ||||||
`useImperativeHandle` は、[ref](/learn/manipulating-the-dom-with-refs) として公開されるハンドルをカスタマイズするための React フックです。 | ||||||
|
||||||
```js | ||||||
useImperativeHandle(ref, createHandle, dependencies?) | ||||||
|
@@ -16,11 +16,11 @@ useImperativeHandle(ref, createHandle, dependencies?) | |||||
|
||||||
--- | ||||||
|
||||||
## Reference {/*reference*/} | ||||||
## リファレンス {/*reference*/} | ||||||
|
||||||
### `useImperativeHandle(ref, createHandle, dependencies?)` {/*useimperativehandle*/} | ||||||
|
||||||
Call `useImperativeHandle` at the top level of your component to customize the ref handle it exposes: | ||||||
`useImperativeHandle` をコンポーネントのトップレベルで呼び出し、公開される ref ハンドルをカスタマイズします。 | ||||||
|
||||||
```js | ||||||
import { forwardRef, useImperativeHandle } from 'react'; | ||||||
|
@@ -34,27 +34,27 @@ const MyInput = forwardRef(function MyInput(props, ref) { | |||||
// ... | ||||||
``` | ||||||
|
||||||
[See more examples below.](#usage) | ||||||
[さらに例を見る](#usage) | ||||||
|
||||||
#### Parameters {/*parameters*/} | ||||||
#### 引数 {/*parameters*/} | ||||||
|
||||||
* `ref`: The `ref` you received as the second argument from the [`forwardRef` render function.](/reference/react/forwardRef#render-function) | ||||||
* `ref`: [`forwardRef` render 関数](/reference/react/forwardRef#render-function)から 2 番目の引数として受け取った `ref` です。 | ||||||
|
||||||
* `createHandle`: A function that takes no arguments and returns the ref handle you want to expose. That ref handle can have any type. Usually, you will return an object with the methods you want to expose. | ||||||
* `createHandle`: 引数を受け取らず、公開したい ref ハンドルを返す関数です。ref ハンドルは任意の型が使えます。通常、公開したいメソッドを持つオブジェクトを返します。 | ||||||
|
||||||
* **optional** `dependencies`: The list of all reactive values referenced inside of the `createHandle` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. If a re-render resulted in a change to some dependency, or if you omitted this argument, your `createHandle` function will re-execute, and the newly created handle will be assigned to the ref. | ||||||
* **オプション** `dependencies`: `createHandle` のコード内でリファレンスされるすべてのリアクティブな値のリストです。リアクティブな値には、props、state、およびコンポーネント内で直接宣言された変数と関数が含まれます。もし linter が [React 向けに設定](/learn/editor-setup#linting)されている場合、linter がすべてのリアクティブな値が依存関係として正しく指定されているかを確認します。依存関係のリストは、一定数の項目があり、[dep1, dep2, dep3]のようにインラインで記述される必要があります。React は、[`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) の比較メソッドを使用して、各依存関係を以前の値と比較します。もし再レンダーが依存関係のいずれかに変更をもたらした場合、またはその引数を省略した場合、`createHandle` 関数が再実行され、新しく作成されたハンドルが ref に割り当てられます。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
useEffect などすでに翻訳済みのほうと訳文を合わせました |
||||||
|
||||||
#### Returns {/*returns*/} | ||||||
#### 返り値 {/*returns*/} | ||||||
|
||||||
`useImperativeHandle` returns `undefined`. | ||||||
`useImperativeHandle` は `undefined` を返します。 | ||||||
|
||||||
--- | ||||||
|
||||||
## Usage {/*usage*/} | ||||||
## 使用方 {/*usage*/} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
### Exposing a custom ref handle to the parent component {/*exposing-a-custom-ref-handle-to-the-parent-component*/} | ||||||
### 親コンポーネントにカスタム ref ハンドルを公開する {/*exposing-a-custom-ref-handle-to-the-parent-component*/} | ||||||
|
||||||
By default, components don't expose their DOM nodes to parent components. For example, if you want the parent component of `MyInput` to [have access](/learn/manipulating-the-dom-with-refs) to the `<input>` DOM node, you have to opt in with [`forwardRef`:](/reference/react/forwardRef) | ||||||
デフォルトでは、コンポーネントはその DOM ノードを親コンポーネントに公開しません。例えば、`MyInput` の親コンポーネントが `<input>` DOM ノードに[アクセスできるように](/learn/manipulation-the-dom-with-refs)したい場合は、[`forwardRef`](/reference/react/forwardRef) にオプトインする必要があります。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```js {4} | ||||||
import { forwardRef } from 'react'; | ||||||
|
@@ -64,25 +64,25 @@ const MyInput = forwardRef(function MyInput(props, ref) { | |||||
}); | ||||||
``` | ||||||
|
||||||
With the code above, [a ref to `MyInput` will receive the `<input>` DOM node.](/reference/react/forwardRef#exposing-a-dom-node-to-the-parent-component) However, you can expose a custom value instead. To customize the exposed handle, call `useImperativeHandle` at the top level of your component: | ||||||
上記のコードでは、[`MyInput` の ref は `<input>` DOM ノードを受け取ります。](/reference/react/forwardRef#exposing-a-dom-node-to-the-parent-component)ただし、代わりにカスタムな値を公開することもできます。公開されるハンドルをカスタマイズするには、コンポーネントのトップレベルで `useImperativeHandle` を呼び出します。 | ||||||
|
||||||
```js {4-8} | ||||||
import { forwardRef, useImperativeHandle } from 'react'; | ||||||
|
||||||
const MyInput = forwardRef(function MyInput(props, ref) { | ||||||
useImperativeHandle(ref, () => { | ||||||
return { | ||||||
// ... your methods ... | ||||||
// ... あなたのメソッド ... | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
よほど理解に支障のある長文などでない限りは訳さないルールにしています |
||||||
}; | ||||||
}, []); | ||||||
|
||||||
return <input {...props} />; | ||||||
}); | ||||||
``` | ||||||
|
||||||
Note that in the code above, the `ref` is no longer forwarded to the `<input>`. | ||||||
上記のコードでは、`ref` が `<input>` に受け渡しされなくなっていることに注意してください。 | ||||||
|
||||||
For example, suppose you don't want to expose the entire `<input>` DOM node, but you want to expose two of its methods: `focus` and `scrollIntoView`. To do this, keep the real browser DOM in a separate ref. Then use `useImperativeHandle` to expose a handle with only the methods that you want the parent component to call: | ||||||
例えば、`<input>` DOM ノード全体を公開するのではなく、その 2 つのメソッド、`focus` と `scrollIntoView` を公開したいとします。これを行うには、実際のブラウザの DOM を別の ref に保持します。そして、`useImperativeHandle` を使用して、親コンポーネントに呼び出したいメソッドのみを含むハンドルを公開します。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```js {7-14} | ||||||
import { forwardRef, useRef, useImperativeHandle } from 'react'; | ||||||
|
@@ -105,7 +105,7 @@ const MyInput = forwardRef(function MyInput(props, ref) { | |||||
}); | ||||||
``` | ||||||
|
||||||
Now, if the parent component gets a ref to `MyInput`, it will be able to call the `focus` and `scrollIntoView` methods on it. However, it will not have full access to the underlying `<input>` DOM node. | ||||||
これで、親コンポーネントが `MyInput` への ref を取得し、そのコンポーネントで `focus` メソッドと `scrollIntoView` メソッドを呼び出すことができるようになります。ただし、親コンポーネントは基礎となる `<input>` DOM ノードへの完全なアクセス権は持ちません。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
<Sandpack> | ||||||
|
||||||
|
@@ -118,7 +118,7 @@ export default function Form() { | |||||
|
||||||
function handleClick() { | ||||||
ref.current.focus(); | ||||||
// This won't work because the DOM node isn't exposed: | ||||||
// DOM ノードが公開されていないため、これでは機能しません。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// ref.current.style.opacity = 0.5; | ||||||
} | ||||||
|
||||||
|
@@ -166,9 +166,9 @@ input { | |||||
|
||||||
--- | ||||||
|
||||||
### Exposing your own imperative methods {/*exposing-your-own-imperative-methods*/} | ||||||
### 独自の命令型メソッドを公開する {/*exposing-your-own-imperative-methods*/} | ||||||
|
||||||
The methods you expose via an imperative handle don't have to match the DOM methods exactly. For example, this `Post` component exposes a `scrollAndFocusAddComment` method via an imperative handle. This lets the parent `Page` scroll the list of comments *and* focus the input field when you click the button: | ||||||
命令型ハンドルを介して公開するメソッドは、DOM メソッドと正確に一致する必要はありません。例えば、この `Post` コンポーネントは、命令型ハンドルを介して `scrollAndFocusAddComment` メソッドを公開します。これにより、ボタンをクリックすると、親である `Page` がコメントのリストをスクロールできる*だけでなく*、入力フィールドにフォーカスもできるようになります。 | ||||||
|
||||||
<Sandpack> | ||||||
|
||||||
|
@@ -281,8 +281,8 @@ export default AddComment; | |||||
|
||||||
<Pitfall> | ||||||
|
||||||
**Do not overuse refs.** You should only use refs for *imperative* behaviors that you can't express as props: for example, scrolling to a node, focusing a node, triggering an animation, selecting text, and so on. | ||||||
**ref の過度な使用に注意してください。**ref は、prop として表現できない、*命令形式*の動作にのみ使用するべきです。例えば、ノードへのスクロール、ノードへのフォーカス、アニメーションのトリガ、テキストの選択などです。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
**If you can express something as a prop, you should not use a ref.** For example, instead of exposing an imperative handle like `{ open, close }` from a `Modal` component, it is better to take `isOpen` as a prop like `<Modal isOpen={isOpen} />`. [Effects](/learn/synchronizing-with-effects) can help you expose imperative behaviors via props. | ||||||
**何かを prop として表現できる場合は、ref を使用すべきではありません。**例えば、`Modal` コンポーネントから `{ open, close }` のような命令形式のハンドルを公開するのではなく、`<Modal isOpen={isOpen} />`のように、`isOpen` を prop として受け取る方が良いでしょう。[エフェクト](/learn/synchronizing-with-effects)を使用することで、命令形式の動作をプロップとして公開することができます。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
この文脈ではあくまでエフェクトは補助的な道具に過ぎないので最後の部分は "help" のニュアンスを加えました。 |
||||||
|
||||||
</Pitfall> |
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.