该decorateAPI 已在 MobX 6 中移除,需要makeObservable在目标类的构造函数中替换。它接受相同的参数。
例子:
import { makeObservable, observable, computed, action } from "mobx"
class Doubler {
value
constructor(value) {
makeObservable(this, {
value: observable,
double: computed,
increment: action
})
this.value = value
}
get double() {
return this.value * 2
}
increment() {
this.value++
}
}
Run Code Online (Sandbox Code Playgroud)
还有新东西makeAutoObservable,你甚至不需要使用装饰器:
import { makeAutoObservable } from "mobx"
class Timer {
// You don't even need to use decorators anymore
secondsPassed = 0
constructor() {
// Call it here
makeAutoObservable(this)
}
increaseTimer() {
this.secondsPassed += 1
}
}
Run Code Online (Sandbox Code Playgroud)
更多信息在这里:
https://mobx.js.org/react-integration.html
https://mobx.js.org/migrating-from-4-or-5.html
| 归档时间: |
|
| 查看次数: |
3400 次 |
| 最近记录: |