Created
December 31, 2018 11:52
-
-
Save tsapeta/6ef4d51cfaeb786b4c94f717b3738162 to your computer and use it in GitHub Desktop.
Expo TaskManager example with EventEmitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { TaskManager } from 'expo'; | |
import { EventEmitter } from 'fbemitter'; | |
const taskName = 'task-name-of-your-choice'; | |
const taskEventName = 'task-update'; | |
const eventEmitter = new EventEmitter(); | |
TaskManager.defineTask(taskName, ({ data, error }) => { | |
if (!error) { | |
// ... do something with given data (e.g. save to the storage) to keep data captured in the background | |
eventEmitter.emit(taskEventName, data); | |
} | |
}); | |
export default class MyComponent extends React.Component { | |
state = { | |
taskData: null, | |
}; | |
componentDidMount() { | |
// ... restore data captured in the background | |
this.eventSubscription = eventEmitter.addListener(taskEventName, data => { | |
this.setState({ taskData: data }); | |
}); | |
} | |
componentWillUnmount() { | |
this.eventSubscription.remove(); | |
} | |
// ... render stuff | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment