mobx 反应不稳定,循环函数
mobx reaction unstable, cyclic function
我正在尝试使用 MobX 来创建 Otello 游戏。所以我有反应寻找瓷砖变化,然后相应地更新其他瓷砖。
所以在下面的代码中,我运行 bumpRandom()
换了另一个tile 看看效果。但是这会进入循环函数,因为 reaction
总是 运行。如何让它在 reaction
中停止观察?
class OtelloBoard {
@observable cells = [];
constructor() {
for (i = 0; i< SIZE*SIZE; i++) {
this.cells.push(new Cell())
}
reaction(
() => this.cells.map( cell => cell.status ),
status => {
this.bumpRandom()
console.log('Status has changed' + status)
}
)
}
@action bumpRandom() {
this.cells[45].bump()
}
}
我尝试了 untracked(() => this.bumpRandom())
,但这也不起作用。
在this MobX issue讨论后,我找到了不使用反应的解决方案。我正在使用 @action
,并使用 运行 onClick()
class OtelloBoard {
@observable cells = [];
constructor() {
for (i = 0; i< SIZE*SIZE; i++) {
this.cells.push(new Cell())
}
}
@action bumpRandom() {
this.cells[45].bump()
}
}
我正在尝试使用 MobX 来创建 Otello 游戏。所以我有反应寻找瓷砖变化,然后相应地更新其他瓷砖。
所以在下面的代码中,我运行 bumpRandom()
换了另一个tile 看看效果。但是这会进入循环函数,因为 reaction
总是 运行。如何让它在 reaction
中停止观察?
class OtelloBoard {
@observable cells = [];
constructor() {
for (i = 0; i< SIZE*SIZE; i++) {
this.cells.push(new Cell())
}
reaction(
() => this.cells.map( cell => cell.status ),
status => {
this.bumpRandom()
console.log('Status has changed' + status)
}
)
}
@action bumpRandom() {
this.cells[45].bump()
}
}
我尝试了 untracked(() => this.bumpRandom())
,但这也不起作用。
在this MobX issue讨论后,我找到了不使用反应的解决方案。我正在使用 @action
,并使用 运行 onClick()
class OtelloBoard {
@observable cells = [];
constructor() {
for (i = 0; i< SIZE*SIZE; i++) {
this.cells.push(new Cell())
}
}
@action bumpRandom() {
this.cells[45].bump()
}
}