Update Timers.md

This commit is contained in:
Christopher Chedeau 2015-02-23 20:04:02 -08:00
parent 394ddd5d69
commit 83cdba1b08
1 changed files with 21 additions and 19 deletions

View File

@ -13,25 +13,6 @@ Timers are an important part of an application and React Native implements the [
The `Promise` implementation uses `setImmediate` its asynchronicity primitive. The `Promise` implementation uses `setImmediate` its asynchronicity primitive.
## TimerMixin
We found out that the primary cause of fatals in apps created with React Native was due to timers firing after a component was unmounted. To solve this recurring issue, we introduced `TimerMixin`. If you include `TimerMixin`, then you can replace your calls to `setTimeout(fn, 500)` with `this.setTimeout(fn, 500)` (just prepend `this.`) and everything will be properly cleaned up for you when the component unmounts.
```javascript
var { TimerMixin } = React;
var Component = React.createClass({
mixins: [TimerMixin],
componentDidMount: function() {
this.setTimeout(
() => { console.log('I do not leak!'); },
500
);
}
});
```
We highly recommend never using bare timers and always using this mixin, it will save you from a lot of hard to track down bugs.
## InteractionManager ## InteractionManager
@ -62,3 +43,24 @@ var handle = InteractionManager.createInteractionHandle();
InteractionManager.clearInteractionHandle(handle); InteractionManager.clearInteractionHandle(handle);
// queued tasks run if all handles were cleared // queued tasks run if all handles were cleared
``` ```
## TimerMixin
We found out that the primary cause of fatals in apps created with React Native was due to timers firing after a component was unmounted. To solve this recurring issue, we introduced `TimerMixin`. If you include `TimerMixin`, then you can replace your calls to `setTimeout(fn, 500)` with `this.setTimeout(fn, 500)` (just prepend `this.`) and everything will be properly cleaned up for you when the component unmounts.
```javascript
var { TimerMixin } = React;
var Component = React.createClass({
mixins: [TimerMixin],
componentDidMount: function() {
this.setTimeout(
() => { console.log('I do not leak!'); },
500
);
}
});
```
We highly recommend never using bare timers and always using this mixin, it will save you from a lot of hard to track down bugs.