Update index.md (#19452)

This commit is contained in:
The Coding Aviator
2018-10-16 14:14:34 +05:30
committed by Randell Dawson
parent d7ba56a3d5
commit 66d95eda48

View File

@ -3,51 +3,23 @@ title: 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:
```javascript
componentDidMount() {
setTimeout( () => {
this.setState({
one: 1,
two: false
});
}, interval);
}
```
where ``` one ``` and ``` two ``` are states you want to set after ``` interval ```ms.
### Hint 1
### Hint
Log something to the console in the ```react componentWillMount ``` method.
Use
```javascript
this.state.stateName
```
and change ``` stateName ``` as required.
### Hint 2
The `` console.log(); `` statement is used to log to the browser console.
### Solution
Change
```javascript
render() {
return (
<div>
<h1>Active Users: { /* change code here */ }</h1>
</div>
);
}
```
to
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: { this.state.activeUsers }</h1>
</div>
);
}
componentWillMount() {
console.log('Component being mounted');
}
```
##### 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)