You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/render-props.md
+34-34
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,28 @@
1
1
---
2
2
id: render-props
3
-
title: Render Props
3
+
title: レンダープロップ
4
4
permalink: docs/render-props.html
5
5
---
6
6
7
-
The term ["render prop"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce) refers to a technique for sharing code between React components using a prop whose value is a function.
Libraries that use render props include [React Router](https://reacttraining.com/react-router/web/api/Route/Route-render-methods)and[Downshift](https://github.com/paypal/downshift).
In this document, we’ll discuss why render props are useful, and how to write your own.
19
+
このドキュメントでは、レンダープロップが役立つ理由と、その実装手順について解説します。
20
20
21
-
## Use Render Props for Cross-Cutting Concerns
21
+
## 横断的関心事にレンダープロップを使う
22
22
23
-
Components are the primary unit of code reuse in React, but it's not always obvious how to share the state or behavior that one component encapsulates to other components that need that same state.
23
+
コンポーネントは、React でコードを再利用するための主な要素ですが、あるコンポーネントがカプセル化した state や振る舞いを、同じ state を必要とする別のコンポーネントと共有する方法については、あまり自明ではありません。
24
24
25
-
For example, the following component tracks the mouse position in a web app:
25
+
たとえば、以下のコンポーネントは、ウェブアプリケーション内でのマウスの位置を追跡します。
26
26
27
27
```js
28
28
classMouseTrackerextendsReact.Component {
@@ -50,11 +50,11 @@ class MouseTracker extends React.Component {
50
50
}
51
51
```
52
52
53
-
As the cursor moves around the screen, the component displays its (x, y) coordinates in a `<p>`.
Now the question is: How can we reuse this behavior in another component? In other words, if another component needs to know about the cursor position, can we encapsulate that behavior so that we can easily share it with that component?
Since components are the basic unit of code reuse in React, let's try refactoring the code a bit to use a `<Mouse>`component that encapsulates the behavior we need to reuse elsewhere.
// The <Mouse> component encapsulates the behavior we need...
@@ -95,11 +95,11 @@ class MouseTracker extends React.Component {
95
95
}
96
96
```
97
97
98
-
Now the `<Mouse>`component encapsulates all behavior associated with listening for `mousemove`events and storing the (x, y) position of the cursor, but it's not yet truly reusable.
For example, let's say we have a `<Cat>`component that renders the image of a cat chasing the mouse around the screen. We might use a `<Cat mouse={{ x, y }}>`prop to tell the component the coordinates of the mouse so it knows where to position the image on the screen.
100
+
たとえば、画面の中でマウスを追いかける猫の画像をレンダーする `<Cat>`コンポーネントがあるとしましょう。`<Cat mouse={{ x, y }}>`props を使って、このコンポーネントにマウスの座標を受け渡し、画面上のどこに猫の画像を配置すれば良いかを知らせたいでしょう。
101
101
102
-
As a first pass, you might try rendering the `<Cat>`*inside `<Mouse>`'s `render`method*, like this:
@@ -153,9 +153,9 @@ class MouseTracker extends React.Component {
153
153
}
154
154
```
155
155
156
-
This approach will work for our specific use case, but we haven't achieved the objective of truly encapsulating the behavior in a reusable way. Now, every time we want the mouse position for a different use case, we have to create a new component (i.e. essentially another `<MouseWithCat>`) that renders something specifically for that use case.
Here's where the render prop comes in: Instead of hard-coding a `<Cat>`inside a `<Mouse>`component, and effectively changing its rendered output, we can provide `<Mouse>`with a function prop that it uses to dynamically determine what to render–a render prop.
@@ -209,13 +209,13 @@ class MouseTracker extends React.Component {
209
209
}
210
210
```
211
211
212
-
Now, instead of effectively cloning the `<Mouse>`component and hard-coding something else in its `render`method to solve for a specific use case, we provide a `render` prop that `<Mouse>`can use to dynamically determine what it renders.
This technique makes the behavior that we need to share extremely portable. To get that behavior, render a`<Mouse>`with a `render` prop that tells it what to render with the current (x, y) of the cursor.
One interesting thing to note about render props is that you can implement most [higher-order components](/docs/higher-order-components.html) (HOC) using a regular component with a render prop. For example, if you would prefer to have a `withMouse` HOC instead of a `<Mouse>`component, you could easily create one using a regular `<Mouse>`with a render prop:
218
+
レンダープロップの興味深い点として、多くの[高階コンポーネント](/docs/higher-order-components.html) (HOC) がレンダープロップを使った通常のコンポーネントによって実装可能ということが挙げられます。たとえば、`<Mouse>`コンポーネントよりも `withMouse` HOC が好みであれば、レンダープロップを持つ `<Mouse>`を使って簡単に作成可能です。
219
219
220
220
```js
221
221
// If you really want a HOC for some reason, you can easily
@@ -233,21 +233,21 @@ function withMouse(Component) {
233
233
}
234
234
```
235
235
236
-
So using a render prop makes it possible to use either pattern.
236
+
つまり、レンダープロップによってどちらのパターンも可能になります。
237
237
238
-
## Using Props Other Than `render`
238
+
## `render` 以外の props を使う
239
239
240
-
It's important to remember that just because the pattern is called "render props" you don't *have to use a prop named `render`to use this pattern*. In fact, [*any* prop that is a function that a component uses to know what to render is technically a "render prop"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce).
And remember, the `children`prop doesn't actually need to be named in the list of "attributes" in your JSX element. Instead, you can put it directly *inside* the element!
@@ -257,23 +257,23 @@ And remember, the `children` prop doesn't actually need to be named in the list
257
257
</Mouse>
258
258
```
259
259
260
-
You'll see this technique used in the [react-motion](https://github.com/chenglou/react-motion) API.
260
+
このテクニックは、[react-motion](https://github.com/chenglou/react-motion)の API などで使用されています。
261
261
262
-
Since this technique is a little unusual, you'll probably want to explicitly state that `children`should be a function in your `propTypes`when designing an API like this.
262
+
このテクニックは若干珍しいため、このような API 設計時には、`children`が関数であることを `propTypes`で明示した方が良いでしょう。
263
263
264
264
```js
265
265
Mouse.propTypes= {
266
266
children:PropTypes.func.isRequired
267
267
};
268
268
```
269
269
270
-
## Caveats
270
+
## 注意事項
271
271
272
-
### Be careful when using Render Props with React.PureComponent
272
+
### レンダープロップを React.PureComponent で使うときの注意点
273
273
274
-
Using a render prop can negate the advantage that comes from using [`React.PureComponent`](/docs/react-api.html#reactpurecomponent)if you create the function inside a `render` method. This is because the shallow prop comparison will always return `false`for new props, and each `render`in this case will generate a new value for the render prop.
For example, continuing with our `<Mouse>`component from above, if `Mouse`were to extend `React.PureComponent` instead of `React.Component`, our example would look like this:
@@ -299,9 +299,9 @@ class MouseTracker extends React.Component {
299
299
}
300
300
```
301
301
302
-
In this example, each time `<MouseTracker>`renders, it generates a new function as the value of the `<Mouse render>`prop, thus negating the effect of `<Mouse>`extending`React.PureComponent` in the first place!
@@ -322,4 +322,4 @@ class MouseTracker extends React.Component {
322
322
}
323
323
```
324
324
325
-
In cases where you cannot define the prop statically (e.g. because you need to close over the component's props and/or state) `<Mouse>`should extend `React.Component`instead.
325
+
props を静的に定義できない場合(たとえば、コンポーネントの props や state をクロージャで囲む場合など)、`<Mouse>`は代わりに `React.Component`を継承すべきです。
0 commit comments