Skip to content

Commit 9277f06

Browse files
author
Dustin Masters
committed
Update constructor docs to include context
1 parent 361e27c commit 9277f06

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

content/docs/reference-react-component.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,18 @@ render() {
136136
### `constructor()`
137137

138138
```javascript
139-
constructor(props)
139+
constructor(props, context)
140140
```
141141

142-
The constructor for a React component is called before it is mounted. When implementing the constructor for a `React.Component` subclass, you should call `super(props)` before any other statement. Otherwise, `this.props` will be undefined in the constructor, which can lead to bugs.
142+
The constructor for a React component is called before it is mounted. When implementing the constructor for a `React.Component` subclass, you should call `super(props)` before any other statement. Otherwise, `this.props` will be undefined in the constructor, which can lead to bugs. Optionally, if you need access to context in your component, call `super(props, context)`.
143143

144144
The constructor is the right place to initialize state. If you don't initialize state and you don't bind methods, you don't need to implement a constructor for your React component.
145145

146146
It's okay to initialize state based on props. This effectively "forks" the props and sets the state with the initial props. Here's an example of a valid `React.Component` subclass constructor:
147147

148148
```js
149-
constructor(props) {
150-
super(props);
149+
constructor(props, context) {
150+
super(props, context);
151151
this.state = {
152152
color: props.initialColor
153153
};
@@ -269,7 +269,7 @@ A class component becomes an error boundary if it defines this lifecycle method.
269269
For more details, see [*Error Handling in React 16*](/blog/2017/07/26/error-handling-in-react-16.html).
270270

271271
> Note
272-
>
272+
>
273273
> Error boundaries only catch errors in the components **below** them in the tree. An error boundary can’t catch an error within itself.
274274
275275
* * *

0 commit comments

Comments
 (0)