Lightweight React bindings for MobX based on React 16.8+ and Hooks
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
MobX React Lite provides lightweight React bindings for MobX state management, specifically designed for React 16.8+ functional components using Hooks. It offers a smaller and faster alternative to mobx-react by focusing exclusively on functional components and modern React patterns.
npm install mobx-react-litemobx ^6.1.0, react ^16.8.0 || ^17 || ^18import { observer, Observer, useLocalObservable } from "mobx-react-lite";For CommonJS:
const { observer, Observer, useLocalObservable } = require("mobx-react-lite");import { observer, Observer, useLocalObservable } from "mobx-react-lite";
import { observable } from "mobx";
import { useState } from "react";
// Create an observable component
const Counter = observer(() => {
const store = useLocalObservable(() => ({
count: 0,
increment() {
this.count++;
}
}));
return (
<div>
<p>Count: {store.count}</p>
<button onClick={store.increment}>Increment</button>
</div>
);
});
// Use the Observer component for specific regions
const App = () => {
const [state] = useState(() => observable({ count: 0 }));
return (
<div>
<Observer>
{() => <div>Reactive count: {state.count}</div>}
</Observer>
<button onClick={() => state.count++}>Update</button>
</div>
);
};MobX React Lite is built around several key components:
observer function wraps functional components to make them reactive to MobX observables<Observer> component enables reactive regions within non-reactive componentsuseLocalObservable create component-local observable stateCore functionality for making React components reactive to MobX observable changes. The observer pattern automatically tracks observable access and triggers re-renders when dependencies change.
function observer<P extends object>(
baseComponent: React.FunctionComponent<P>
): React.FunctionComponent<P>;
interface IObserverOptions {
readonly forwardRef?: boolean;
}Hooks for creating and managing observable state within React components. Provides component-local observable state that integrates seamlessly with the component lifecycle.
function useLocalObservable<TStore extends Record<string, any>>(
initializer: () => TStore,
annotations?: AnnotationsMap<TStore, never>
): TStore;Server-side rendering utilities that control reactive behavior in SSR environments. These APIs ensure components render correctly on the server without memory leaks.
function enableStaticRendering(enable: boolean): void;
function isUsingStaticRendering(): boolean;Advanced configuration and utilities including observer batching, memory management, and deprecated APIs for migration purposes.
function observerBatching(reactionScheduler: any): void;
function clearTimers(): void;// Observer component props
interface IObserverProps {
children?(): React.ReactElement | null;
render?(): React.ReactElement | null;
}
// Observer options for function overloads
interface IObserverOptions {
readonly forwardRef?: boolean;
}
// MobX annotations from mobx package
type Annotation = {
annotationType_: string;
make_(adm: any, key: PropertyKey, descriptor: PropertyDescriptor, source: object): any;
extend_(adm: any, key: PropertyKey, descriptor: PropertyDescriptor, proxyTrap: boolean): boolean | null;
options_?: any;
};
type AnnotationMapEntry = Annotation | true | false;
type AnnotationsMap<T, AdditionalKeys extends PropertyKey> = {
[K in keyof T | AdditionalKeys]?: AnnotationMapEntry;
};