MobX React Lite

4.1.1 · active · verified Sun Apr 19

mobx-react-lite provides lightweight, performant React bindings specifically designed for functional components and React Hooks, making it a smaller (1.5kB gzipped) and faster alternative to the full `mobx-react` package. It leverages React's modern features, requiring React 16.8 or higher, and offers core functionalities like `observer` for making components reactive, and `useLocalObservable` for managing local observable state within functional components. Unlike its `mobx-react` counterpart, it eschews `Provider`/`inject` in favor of `useContext` for dependency injection. The current stable version is 4.1.1, with recent updates including support for React 19, and its release cadence is closely aligned with MobX and React major version cycles, ensuring compatibility and performance with modern React applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create a reactive functional component using `observer` and manage local observable state with `useLocalObservable`, complete with a MobX store, actions, and computed values.

import React from 'react';
import ReactDOM from 'react-dom/client';
import { observer, useLocalObservable } from 'mobx-react-lite';

const Timer = observer(() => {
  const store = useLocalObservable(() => ({
    secondsPassed: 0,
    increment() {
      this.secondsPassed++;
    },
    reset() {
      this.secondsPassed = 0;
    },
    get formattedTime() {
      return `Seconds: ${this.secondsPassed}`;
    }
  }));

  React.useEffect(() => {
    const handle = setInterval(() => store.increment(), 1000);
    return () => clearInterval(handle);
  }, [store]);

  return (
    <div>
      <h1>MobX Timer</h1>
      <p>{store.formattedTime}</p>
      <button onClick={() => store.reset()}>Reset</button>
    </div>
  );
});

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Timer />);

view raw JSON →