New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mobx reaction unstable, cyclic function #528
Comments
Can you explain what you want to achieve here? What needs to happen automatically here? To me this code reads as 'play the entire game', which mobx tries to do but then it sniffs an endless loop. Should only one bump happen? Or only a bump when there is a specific status? |
So when a tile is bumped, the reaction is called, it will then update another tile (bump). But this update should not trigger another reaction until it's finished. Maybe I should wrap them up in |
what is 'is finished'? you reaction is now configured to react to any change of status inside one of the cells |
So I'm wanting to update other cells within the code block but I don't want that to cause a reaction. The reaction should only be triggered when user touches the cell.
Is there a better way of doing what I want?
|
yep that shouldn't be a reaction in the first place, it sounds it sound triggered by a state change, but by a user event, in otherwords, an action. The idea of a reaction is that you can tell by just looking at the state what reactions should happen, which doesn't seem to be the case in your example, the reaction doesn't depend on the actual value of the |
Actually, I probably did not phrase it right.
I'm relying on MobX to observe for state change. ie. when user touches the cell, the Maybe I shouldn't use mobx in this case? |
@hiaw, you have a reaction, that depends on every cell, and when it fire it randomly changes cell, so will fire again untill it reaches stable state, which can't be achieved. transaction may help, but whats the point of using reaction here? You can simply call class OtelloBoard {
@observable cells = [];
@observable move = 0;
constructor() {
for (i = 0; i< SIZE*SIZE; i++) {
this.cells.push(new Cell())
}
reaction(
() => this.move,
move => {
if (move % 2) {
// computer move
this.makeMove(Math.round(SIZE*SIZE * Math.random()))
} else {
// do nothing
}
}
)
}
@action makeMove(cellNum) {
this.cells[cellNum].status = !this.cells[cellNum].status
this.move++;
}
} |
Yeah. I think you're right on using the click handler. Thank you very much for your help. |
I'm trying to use MobX to create an Otello game. So I have reaction to look for tile changes and then update other tiles accordingly.
So in the code below, I ran
bumpRandom()
to change another tile to see the effect. But then this goes into cyclic function causereaction
is alway ran. How do I get it to stop observing in areaction
?I tried
untracked(() => this.bumpRandom())
but that doesn't work either.The text was updated successfully, but these errors were encountered: