-
-
Save ChronSyn/42a31e365900900b96bf525ca4b7fe1d 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