Mobx

  • declarative, reactive programming

  • different paradigm then redux/zustand

Diff from Zustand/Redux

  • Instead of selectors, just wrap functional component in observers if it relies on state

  • Need to pass store down yourself either just directly or useContext

// store.js

import { makeAutoObservable } from "mobx";

class CounterStore {
    value = 0;

    constructor() {
        makeAutoObservable(this);
    }

    increment() {
        this.value++;
    }
}

export const counterStore = new CounterStore();

// CounterComponent.js

Advanced

  • autorun: Automatically tracks any observable accessed within its function and re-executes when they change.

  • reaction: Tracks a specific observable and runs side-effects when it changes.

  • when: Executes a function when a specific condition becomes true.

Last updated