Skip to content

Commit 58b8773

Browse files
committed
💥 Add React Class Based Timeout
1 parent aaf9722 commit 58b8773

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Using `setTimeout` in React Class Components
2+
3+
```javascript
4+
class TimeoutWrapper extends Component {
5+
constructor(props) {
6+
super(props);
7+
8+
// Initialise the timeout state
9+
this.state = {
10+
timeout: null
11+
};
12+
}
13+
14+
userTimeout() {
15+
const { timeout } = this.state;
16+
// Clear any previous timeouts
17+
clearTimeout(timeout);
18+
// Set our new timeout
19+
this.setState({
20+
timeout: setTimeout(() => {
21+
// Do some fun timeout stuff here :)
22+
}, 1000)
23+
});
24+
}
25+
}
26+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ nav:
4444
- "Debounce": "engineering/javascript/snippets/debounce.md"
4545
- React:
4646
- "React DevTools": "engineering/javascript/react/react-devtools.md"
47+
- "React setTimeout": "engineering/javascript/react/react-class-timeout.md"
4748
- NPM:
4849
- "NVM QuickStart": "engineering/javascript/npm/nvm.md"
4950
- "NPM Tips & Snippets": "engineering/javascript/npm/npm-tips.md"

0 commit comments

Comments
 (0)