Skip to content

Instantly share code, notes, and snippets.

@aaronblondeau
Last active May 1, 2018 21:56
Show Gist options
  • Save aaronblondeau/0fd4dfb66571559eeff7e4b7b7df1088 to your computer and use it in GitHub Desktop.
Save aaronblondeau/0fd4dfb66571559eeff7e4b7b7df1088 to your computer and use it in GitHub Desktop.
Application state refactored with observables to hold routes and an action to fetch them
import {observable, action} from 'mobx';
class ApplicationState {
@observable things = [];
@observable loading_things = false;
@observable things_error = "";
@action getThings() {
this.things = [];
this.loading_things = true;
return fetch('https://s3.amazonaws.com/aaronblondeau/react-native-quickly-1/things.json')
.then((response) => response.json())
.then(action((responseJson) => {
this.loading_things = false;
this.things = responseJson;
}))
.catch(action((error) => {
this.things_error = error.message;
this.loading_things = false;
console.error(error);
}));
}
}
const state = new ApplicationState()
export default state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment