Aren't reactions batched? #3414
Answered
by
urugator
gonzobard777
asked this question in
Q&A
-
Consider the following example: import { makeObservable, computed, observable, reaction } from 'mobx';
const animal = {
name: 'Tom',
type: 'cat',
get full() {
return animal.name + ' ' + animal.type;
},
};
makeObservable(animal, {
name: observable,
type: observable,
full: computed,
});
reaction(
() => animal.full,
(value, prev) => console.log(`change: "${value}", prev "${prev}"`)
);
animal.name = 'Jerry';
animal.type = 'mouse';
console.log(`computed:`, animal.full); result is: change: "Jerry cat", prev "Tom cat"
change: "Jerry mouse", prev "Jerry cat"
computed: Jerry mouse Can you please tell me what I should do so that exactly the same batching is performed for reaction as for computed? |
Beta Was this translation helpful? Give feedback.
Answered by
urugator
May 26, 2022
Replies: 1 comment
-
runInAction(() => {
animal.name = 'Jerry';
animal.type = 'mouse';
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
gonzobard777
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://mobx.js.org/actions.html