Created
April 27, 2020 23:03
-
-
Save sangkukbae12/af333bf3d71210b1efc4b575635076a9 to your computer and use it in GitHub Desktop.
react mobx obervable with actions
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
// Bound Actions | |
import { observable } from "mobx"; | |
class Counter { | |
@observable count = 0; | |
@action.bound | |
increment() { | |
this.count++; | |
} | |
} | |
const counter = new Counter(); | |
counter.increment(); | |
// Asynchronous Actions | |
import { observable, action } from "mobx"; | |
import * as mobx from "mobx"; | |
mobx.configure({ enforceActions: "observed" }); | |
class Joker { | |
@observable joke = {}; | |
@observable state = "pending"; | |
@action | |
async fetchJoke() { | |
try { | |
const response = await fetch("https://api.icndb.com/jokes/random"); | |
this.joke = response.json(); | |
mobx.runInAction(() => { | |
this.state = "success"; | |
}); | |
} catch { | |
mobx.runInAction(() => { | |
this.state = "error"; | |
}); | |
} | |
} | |
} | |
const joker = new Joker(); | |
joker.fetchJoke(); | |
// flows | |
import { observable, action } from "mobx"; | |
import * as mobx from "mobx"; | |
mobx.configure({ enforceActions: "observed" }); | |
class Joker { | |
@observable joke = {}; | |
@observable state = "pending"; | |
fetchJoke = mobx.flow(function*() { | |
try { | |
const response = yield fetch("https://api.icndb.com/jokes/random"); | |
this.joke = response.json(); | |
this.state = "success"; | |
} catch { | |
this.state = "error"; | |
} | |
}); | |
} | |
const joker = new Joker(); | |
joker.fetchJoke(); | |
// Promises with Then | |
import { observable, action } from "mobx"; | |
import * as mobx from "mobx"; | |
mobx.configure({ enforceActions: "observed" }); | |
class Joker { | |
@observable joke = {}; | |
@action.bound | |
fetchJokeSuccess(response) { | |
this.joke = response.json(); | |
} | |
@action | |
fetchJoke() { | |
this.joke = {}; | |
fetch("https://api.icndb.com/jokes/random").then(this.fetchJokeSuccess); | |
} | |
} | |
const joker = new Joker(); | |
joker.fetchJoke(); | |
// Promises with Then | |
import { observable, action } from "mobx"; | |
import * as mobx from "mobx"; | |
mobx.configure({ enforceActions: "observed" }); | |
class Joker { | |
@observable joke = {}; | |
@action.bound | |
fetchJokeSuccess(response) { | |
this.joke = response.json(); | |
} | |
@action | |
fetchJoke() { | |
this.joke = {}; | |
fetch("https://api.icndb.com/jokes/random").then(this.fetchJokeSuccess); | |
} | |
} | |
const joker = new Joker(); | |
joker.fetchJoke(); | |
import { observable, action } from "mobx"; | |
import * as mobx from "mobx"; | |
mobx.configure({ enforceActions: "observed" }); | |
class Joker { | |
@observable joke = {}; | |
@action | |
fetchJoke() { | |
this.joke = {}; | |
fetch("https://api.icndb.com/jokes/random").then( | |
action("fetchJokeSuccess", response => { | |
this.joke = response.json(); | |
}) | |
); | |
} | |
} | |
const joker = new Joker(); | |
joker.fetchJoke(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment