Last active
May 1, 2018 21:56
-
-
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
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 {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