Update index.md (#19452)
This commit is contained in:
committed by
Randell Dawson
parent
d7ba56a3d5
commit
66d95eda48
@ -3,51 +3,23 @@ title: Use the Lifecycle Method componentWillMount
|
|||||||
---
|
---
|
||||||
## Use the Lifecycle Method componentWillMount
|
## Use the Lifecycle Method componentWillMount
|
||||||
|
|
||||||
This challenges introduces the ``` componentWillMount ``` Lifecycle method. This is used to set state after a giventime period.
|
The challenge is a basic introduction to React's LifeCycle components.
|
||||||
|
These methods are also called Lifecycle Hooks.
|
||||||
|
|
||||||
The syntax for the method is:
|
### Hint 1
|
||||||
```javascript
|
|
||||||
componentDidMount() {
|
|
||||||
setTimeout( () => {
|
|
||||||
this.setState({
|
|
||||||
one: 1,
|
|
||||||
two: false
|
|
||||||
});
|
|
||||||
}, interval);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
where ``` one ``` and ``` two ``` are states you want to set after ``` interval ```ms.
|
|
||||||
|
|
||||||
### Hint
|
Log something to the console in the ```react componentWillMount ``` method.
|
||||||
|
|
||||||
Use
|
### Hint 2
|
||||||
```javascript
|
|
||||||
this.state.stateName
|
The `` console.log(); `` statement is used to log to the browser console.
|
||||||
```
|
|
||||||
and change ``` stateName ``` as required.
|
|
||||||
|
|
||||||
### Solution
|
### Solution
|
||||||
|
|
||||||
Change
|
Since you can log anything, use the ``` console.log(); ``` and pass in a string. The ``` componentWillMount ``` method should look like:
|
||||||
```javascript
|
|
||||||
render() {
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h1>Active Users: { /* change code here */ }</h1>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
to
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
render() {
|
componentWillMount() {
|
||||||
return (
|
console.log('Component being mounted');
|
||||||
<div>
|
}
|
||||||
<h1>Active Users: { this.state.activeUsers }</h1>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
##### Note: The ``` componentWillMount ``` Lifecycle method has been deprecated as of version 17, and does not work on later versions. [(Source)](https://reactjs.org/docs/react-component.html)
|
|
||||||
|
Reference in New Issue
Block a user