Internal Functions

The following methods are all used internally by MobX, and might come in handy in rare cases. But usually MobX offers more declarative alternatives to tackle the same problem. They might come in handy though if you try to extend MobX.

transaction

  • transaction(worker: () => void)

Deprecated, use actions or runInAction instead. Low-level API that can be used to batch state changes. State changes made inside the block won't cause any computations or reactions to run until the end of the block is reached. Nonetheless inspecting a computed value inside a transaction block will still return a consistent value. It is recommended to use action instead, which uses transaction internally.

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 has returned by the worker function. Note: 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(() => {
        numbers.push(1);
        numbers.push(2);
    });
    numbers.push(3);
});
// Prints: '3 numbers!'

untracked

  • untracked(() => { block })

Low-level API that might be useful inside reactions and computations. Any observables accessed in the block won't cause the reaction / computations to be recomputed automatically. However, it is recommended to use action instead, which uses untracked internally.

Untracked allows you to run a piece of code without establishing observers. Like transaction, untracked is automatically applied by (@)action, so usually it makes more sense to use actions than use untracked directly.

const person = observable({
    firstName: "Michel",
    lastName: "Weststrate"
});

autorun(() => {
    console.log(
        person.lastName,
        ",",
        // this untracked block will return the person's firstName without establishing a dependency
        untracked(() => person.firstName)
    );
});
// prints: Weststrate, Michel

person.firstName = "G.K.";
// doesn't print!

person.lastName = "Chesterton";
// prints: Chesterton, G.K.

Atom

Utility class that can be used to create your own observable data structures and hook them up to MobX. Used internally by all observable data types.

Reaction

Utility class that can be used to create your own reactions and hook them up to MobX. Used internally by autorun, reaction (function), etc.

extras.allowStateChanges

  • allowStateChanges(allowStateChanges, () => { block })

Can be used (dis)allow state changes in a certain function. Used internally by action to allow changes, and by computed and observer to disallow state changes.

extras.resetGlobalState

  • resetGlobalState()

Resets MobX internal global state. MobX by defaults fails fast if an exception occurs inside a computation or reaction and refuses to run them again. This function resets MobX to the zero state. Existing spy listeners and the current value of strictMode will be preserved though.

results matching ""

    No results matching ""