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
> This section documents a legacy API. See the [new API](/docs/context.html).
15
+
> 本节记录一个过时的 API。查看 [新的 API](/docs/context.html)。
16
16
17
-
Suppose you have a structure like:
17
+
假设你有这样一个结构:
18
18
19
19
```javascript
20
20
classButtonextendsReact.Component {
@@ -48,7 +48,7 @@ class MessageList extends React.Component {
48
48
}
49
49
```
50
50
51
-
In this example, we manually thread through a `color`prop in order to style the `Button`and`Message`components appropriately. Using context, we can pass this through the tree automatically:
By adding `childContextTypes` and `getChildContext` to `MessageList` (the context provider), React passes the information down automatically and any component in the subtree (in this case, `Button`) can access it by defining `contextTypes`.
If`contextTypes`is not defined, then `context`will be an empty object.
100
+
如果`contextTypes`没有被定义,`context`就会是个空对象。
101
101
102
-
> Note:
102
+
> 注意:
103
103
>
104
-
> `React.PropTypes` has moved into a different package since React v15.5. Please use [the `prop-types`library instead](https://www.npmjs.com/package/prop-types)to define `contextTypes`.
> This section documents a legacy API. See the [new API](/docs/context.html).
110
+
> 本节记录一个过时的 API。查看 [新的 API](/docs/context.html)。
111
111
112
-
Context can also let you build an API where parents and children communicate. For example, one library that works this way is [React Router V4](https://reacttraining.com/react-router):
Before you build components with an API similar to this, consider if there are cleaner alternatives. For example, you can pass entire React components as props if you'd like to.
138
+
在你构建类似的 API 的组件之前,先考虑是否有更简洁的方式。例如,如果你愿意的话,你可以将整个 React 组件作为 props。
139
139
140
-
### Referencing Context in Lifecycle Methods {#referencing-context-in-lifecycle-methods}
> This section documents a legacy API. See the [new API](/docs/context.html).
142
+
> 本节记录一个过时的 API。查看 [新的 API](/docs/context.html)。
143
143
144
-
If`contextTypes` is defined within a component, the following [lifecycle methods](/docs/react-component.html#the-component-lifecycle)will receive an additional parameter, the `context`object:
> This section documents a legacy API. See the [new API](/docs/context.html).
157
+
> 本节记录一个过时的 API。查看 [新的 API](/docs/context.html)。
158
158
159
-
Function components are also able to reference `context` if `contextTypes` is defined as a property of the function. The following code shows a `Button` component written as a function component.
> This section documents a legacy API. See the [new API](/docs/context.html).
176
+
> 本节记录一个过时的 API。查看 [新的 API](/docs/context.html)。
175
177
176
-
Don't do it.
178
+
不要这样做。
177
179
178
-
React has an API to update context, but it is fundamentally broken and you should not use it.
180
+
React 有一个 API 可以更新 context,但它基本上是坏的,你不应该使用它。
179
181
180
-
The `getChildContext` function will be called when the state or props changes. In order to update data in the context, trigger a local state update with `this.setState`. This will trigger a new context and changes will be received by the children.
182
+
当 state 或者 props 改变的时候,`getChildContext` 函数就会被调用。为了更新 context 里的数据,使用 `this.setState` 触发当前 state 的更新。这样会产生一个新的 context 并且子组件会接收到变化。
The problem is, if a context value provided by component changes, descendants that use that value won't update if an intermediate parent returns `false` from `shouldComponentUpdate`. This is totally out of control of the components using context, so there's basically no way to reliably update the context. [This blog post](https://medium.com/@mweststrate/how-to-safely-use-react-context-b7e343eff076) has a good explanation of why this is a problem and how you might get around it.
0 commit comments