MobX React Bindings
mobx-react provides the official React bindings for MobX, enabling the creation of fully reactive React components that automatically re-render when the observable data they consume changes. The current stable version is 9.2.1, which includes support for React 19 and MobX 6.x. This package offers a complete integration, including support for class-based components, the legacy `Provider`/`inject` mechanism, and `PropTypes` utilities for observable-based property checkers. It differentiates itself from `mobx-react-lite` by offering these additional features for existing codebases or specific architectural needs, whereas `mobx-react-lite` is optimized for modern functional components and Hooks with `React.createContext`. Releases often align with new React and MobX versions, providing timely compatibility updates and bug fixes.
Common errors
-
Error: You are trying to observe a component that has already been observed. Please only apply `observer` once.
cause Attempting to wrap a component with `observer` that is already observed or memoized by `React.memo`.fixEnsure `observer` is applied only once to your functional components. Remove any redundant `React.memo` wrappers if present before `observer`. -
Warning: Can't perform a React state update on an unmounted component.
cause A MobX reaction or observable update attempts to trigger a re-render on a component that has been unmounted, typically due to an asynchronous operation completing after the component is gone.fixUse `disposeOnUnmount` for class components or manual cleanup with `useEffect` for functional components to dispose of MobX reactions, autoruns, or other subscriptions when the component unmounts. -
TypeError: Cannot read properties of undefined (reading 'storeName') in component
cause Trying to access a store injected via `Provider`/`inject` when the component is not wrapped in `inject` or is outside a `Provider` context.fixEnsure the component is wrapped with `inject` and rendered within a `Provider` that supplies the necessary stores. For new code, consider `React.createContext` and `useContext` instead.
Warnings
- breaking MobX 4/5 compatibility with mobx-react v5 and earlier. MobX 6+ requires mobx-react v6+.
- deprecated The `Provider` and `inject` API for passing stores through context is considered legacy.
- deprecated MobX-React `PropTypes` for observable-based property checkers are largely superseded.
- gotcha `observer` automatically applies `React.memo` to functional components. Wrapping an already memoized or observed component will throw an error.
- breaking Strict mode compatibility changes, particularly with React 18.2+ require `mobx-react` v9+.
Install
-
npm install mobx-react -
yarn add mobx-react -
pnpm add mobx-react
Imports
- observer
import observer from 'mobx-react';
import { observer } from 'mobx-react'; - Provider
import { Provider } from 'mobx-react'; - inject
import { inject } from 'mobx-react';
Quickstart
import React from 'react';
import ReactDOM from 'react-dom/client';
import { makeAutoObservable } from 'mobx';
import { observer } from 'mobx-react';
class TimerStore {
secondsPassed = 0;
constructor() {
makeAutoObservable(this);
setInterval(() => {
this.secondsPassed++;
}, 1000);
}
reset() {
this.secondsPassed = 0;
}
}
const myTimer = new TimerStore();
const TimerDisplay = observer(() => {
return (
<div>
<p>Seconds passed: {myTimer.secondsPassed}</p>
<button onClick={() => myTimer.reset()}>Reset Timer</button>
</div>
);
});
const root = ReactDOM.createRoot(document.getElementById('root')!);
root.render(
<React.StrictMode>
<TimerDisplay />
</React.StrictMode>
);