Registry / web-framework / mobx-react-lite

mobx-react-lite

JSON →
library4.1.1jsnpmunverified

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.

npm install mobx-react-lite
INSTALL
IMPORT
SIG · MOBX-REACT-LITE
M
mobx-react-lite
web-frameworkjavascriptv4.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

observer
import { observer } from 'mobx-react-lite';
const observer = require('mobx-react-lite').observer;
The primary way to make functional components reactive. CommonJS `require` is generally not recommended for modern React/MobX applications due to ESM preference.
Observer
import { Observer } from 'mobx-react-lite';
import Observer from 'mobx-react-lite/Observer';
A render-prop component used for applying observer behavior to anonymous regions or within class components. It's a named import.
useLocalObservable
import { useLocalObservable } from 'mobx-react-lite';
import { useLocalStore } from 'mobx-react-lite';
`useLocalObservable` is the recommended hook for creating local observable state within functional components. `useLocalStore` is deprecated since v3.

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 />);
Debug
Known issues
breakingmobx-react-lite requires React 16.8 or higher due to its reliance on React Hooks. Older React versions are not supported.
fix
Upgrade your React installation to version 16.8.0 or newer (e.g., `^16.8.0 || ^17 || ^18 || ^19`).
affects: >=1.0.0
deprecatedThe `useObserver` hook is deprecated since mobx-react-lite v3.0.0. Its usage is often incorrect, and the `<Observer>` component provides better decoupling and clarity.
fix
Replace `useObserver(() => <MyComponent />)` with `<Observer>{() => <MyComponent />}</Observer>`. For entire components, use `observer(MyComponent)` directly.
affects: >=3.0.0
deprecatedThe `useLocalStore` hook is deprecated in favor of `useLocalObservable` since mobx-react-lite v3.0.0. While similar, `useLocalObservable` offers clearer semantics and improved TypeScript inference.
fix
Migrate `useLocalStore` calls to `useLocalObservable`. The initializer function's logic remains largely the same.
affects: >=3.0.0
gotchaThis package is designed exclusively for React functional components. For class components, you must use the `mobx-react` package. Using `observer` from `mobx-react-lite` on a class component will not work as expected.
fix
If using class components, install and use `mobx-react`. Alternatively, refactor your class components to functional components to leverage `mobx-react-lite`.
affects: >=1.0.0
gotchaIn server-side rendering (SSR) environments, `observer` wrapped components should not re-render after the initial pass. Failing to configure this can lead to memory leaks or incorrect behavior.
fix
Call `enableStaticRendering(true)` at the very beginning of your SSR entry point to ensure components cleanup after the first render.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
Attempting to use a MobX React Hook (e.g., `useLocalObservable`, `useContext`) outside of a functional React component, or violating other React Hooks rules.
fix
Ensure all MobX React Hooks are called within a functional component, and follow the Rules of Hooks (e.g., call them at the top level, not in loops, conditions, or nested functions).
MyComponent is not re-rendering when observable state changes.
The functional component or a part of its render tree is not wrapped with `observer` or `<Observer>`, preventing MobX from automatically reacting to state changes.
fix
Wrap your functional component with `observer` (e.g., `export const MyComponent = observer(() => { ... });`) or use `<Observer>{() => ...}</Observer>` around the reactive part of your render function.
TypeError: Cannot read properties of undefined (reading 'someProperty') inside an observable store.
The `this` context inside a MobX action or getter within `useLocalObservable` might be unbound if `autoBind: true` is not explicitly set or the function is defined in a way that loses `this`.
fix
`useLocalObservable` by default sets `autoBind: true`. Ensure your initializer functions and actions are defined correctly, often as arrow functions or using the concise method syntax in an object literal, so `this` correctly refers to the observable object. If explicitly creating the observable with `observable()`, remember to pass `{ autoBind: true }` in options.
Upgrade
Version history
4.1.1latest on npm
Audit
Dependencies
mobxrequiredCore reactive state management library.
reactrequiredRuntime peer dependency for React component rendering and Hooks API.
Agent activity
2 hits · last 30 days
node
2
Resources