Skip to content

Commit 2f42f9d

Browse files
committed
Add timer usage for es6
1 parent 1f501a9 commit 2f42f9d

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

docs/Timers.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,25 @@ var Component = React.createClass({
7373
});
7474
```
7575

76-
We strongly discourage using the global `setTimeout(...)` and recommend instead that you use `this.setTimeout(...)` provided by react-timer-mixin. This will eliminate a lot of hard work tracking down bugs, such as crashes caused by timeouts firing after a component has been unmounted.
76+
This will eliminate a lot of hard work tracking down bugs, such as crashes caused by timeouts firing after a component has been unmounted.
77+
78+
However, `TimerMixin` and any other mixins *can not be used in ES6*. But don't worry, what TimerMixin does is really simple - clearing every timer used when current component is going to unmount. So you can manually do this like the following example:
79+
80+
```javascript
81+
import React,{
82+
Component
83+
} from 'react-native';
84+
85+
export default class Hello extends Component {
86+
componentDidMount() {
87+
this.timer = setTimeout(
88+
() => { console.log('save the timer ref to this, which can be cleared later'); },
89+
500
90+
);
91+
}
92+
componentWillUnmount() {
93+
// if this.timer exits, then use clearTimeout to clear it.
94+
this.timer && clearTimeout(this.timer);
95+
}
96+
};
97+
```

0 commit comments

Comments
 (0)