Skip to content

Instantly share code, notes, and snippets.

@tsapeta
Created December 31, 2018 11:52
Show Gist options
  • Save tsapeta/6ef4d51cfaeb786b4c94f717b3738162 to your computer and use it in GitHub Desktop.
Save tsapeta/6ef4d51cfaeb786b4c94f717b3738162 to your computer and use it in GitHub Desktop.
Expo TaskManager example with EventEmitter
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