Files

64 lines
999 B
Markdown
Raw Normal View History

---
title: Use the Lifecycle Method componentDidMount
---
# Use the Lifecycle Method componentDidMount
---
## Problem Explanation
2018-10-16 14:23:57 +05:30
This challenges introduces the ``` componentDidMount ``` Lifecycle method. This is used to set state after a giventime period.
2018-10-16 14:23:57 +05:30
The syntax for the method is:
```jsx
2018-10-16 14:23:57 +05:30
componentDidMount() {
setTimeout( () => {
this.setState({
one: 1,
two: false
});
}, interval);
}
```
where ``` one ``` and ``` two ``` are states you want to set after ``` interval ```ms.
---
## Hints
### Hint 1
2018-10-16 14:23:57 +05:30
Use
```javascript
this.state.stateName;
2018-10-16 14:23:57 +05:30
```
and change ``` stateName ``` as required.
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-16 14:23:57 +05:30
Change
```jsx
2018-10-16 14:23:57 +05:30
render() {
return (
<div>
<h1>Active Users: { /* change code here */ }</h1>
</div>
);
}
```
to
```jsx
2018-10-16 14:23:57 +05:30
render() {
return (
<div>
<h1>Active Users: { this.state.activeUsers }</h1>
</div>
);
}
```
</details>