Edit This Page


Transaction is a low-level api, in most cases action or runInAction are a better alternative

transaction(worker: () => void) can be used to batch a bunch of updates without notifying any observers until the end of the transaction. transaction takes a single, parameterless worker function as argument and runs it. No observers are notified until this function has completed. transaction returns any value that was returned by the worker function. Note that transaction runs completely synchronously. Transactions can be nested. Only after completing the outermost transaction pending reactions will be run.

import {observable, transaction, autorun} from "mobx";

const numbers = observable([]);

autorun(() => console.log(numbers.length, "numbers!"));
// Prints: '0 numbers!'

transaction(() => {
    transaction(() => {
// Prints: '3 numbers!'