mobx-react-lite

Lightweight React bindings for MobX based on function components and Hooks

mobx-react-lite downloads mobx-react-lite version mobx-react-lite license

mobx-react-liteSimilar Packages:

Npm Package Weekly Downloads Trend

3 Years
🌟 Show real-time usage chart on mobx-react-lite's README.md, just copy the code below.
## Usage Trend
[![Usage Trend of mobx-react-lite](https://npm-compare.com/img/npm-trend/THREE_YEARS/mobx-react-lite.png)](https://npm-compare.com/mobx-react-lite#timeRange=THREE_YEARS)

Cumulative GitHub Star Trend

🌟 Show GitHub stars trend chart on mobx-react-lite's README.md, just copy the code below.
## GitHub Stars Trend
[![GitHub Stars Trend of mobx-react-lite](https://npm-compare.com/img/github-trend/mobx-react-lite.png)](https://npm-compare.com/mobx-react-lite)

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
mobx-react-lite3,387,67328,203292 kB55a day agoMIT

README for mobx-react-lite

mobx-react-lite

CircleCI Coverage Status NPM downloadsMinzipped size Discuss on Github View changelog

This package supports React function components only. Use mobx-react if you also need class component support. It is still possible to use <Observer> inside the render of class components.

Compatibility table (major versions)

mobxmobx-react-liteBrowser
75Modern browsers with native Proxy support
63Modern browsers (IE 11+ in compatibility mode)
52Modern browsers
42IE 11+, RN w/o Proxy support

mobx-react-lite version 5 requires React 18 or higher.

User Guide 👉 https://mobx.js.org/react-integration.html


API reference âš’

observer<P>(baseComponent: FunctionComponent<P>): FunctionComponent<P>

The observer converts a component into a reactive component, which tracks which observables are used automatically and re-renders the component when one of these values changes. Can only be used for function components. For class component support see the mobx-react package.

<Observer>{renderFn}</Observer>

Is a React component, which applies observer to an anonymous region in your component. <Observer> can be used both inside class and function components.

useLocalObservable<T>(initializer: () => T, annotations?: AnnotationsMap<T>): T

Creates an observable object with the given properties, methods and computed values.

Note that computed values cannot directly depend on non-observable values, but only on observable values, so it might be needed to sync properties into the observable using useEffect.

useLocalObservable is a short-hand for:

const [state] = useState(() => observable(initializer(), annotations, { autoBind: true }))

enableStaticRendering(enable: true)

Call enableStaticRendering(true) when running in an SSR environment, in which observer wrapped components should never re-render, but cleanup after the first rendering automatically. Use isUsingStaticRendering() to inspect the current setting.


Removed APIs in version 5

useObserver, useLocalStore, useAsObservableSource, useStaticRendering, batching imports, observerBatching, and isObserverBatched have been removed. Use observer, <Observer>, useLocalObservable, and enableStaticRendering; React 18 renderers handle batching.

Testing

Running the full test suite now requires node 14+ But the library itself does not have this limitation

In order to avoid memory leaks due to aborted renders from React fiber handling or React StrictMode, on environments that does not support FinalizationRegistry, this library needs to run timers to tidy up the remains of the aborted renders.

This can cause issues with test frameworks such as Jest which require that timers be cleaned up before the tests can exit.

clearTimers()

Call clearTimers() in the afterEach of your tests to ensure that mobx-react-lite cleans up immediately and allows tests to exit.