mobx-react-lite
Lightweight React bindings for MobX based on React 16.8 and Hooks
Last updated 7 years ago by fredyc .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install mobx-react-lite 
SYNC missed versions from official npm registry.

mobx-react-lite

CircleCICoverage StatusNPM downloadsMinzipped size

TypeScriptcode style: prettier

Join the chat at https://gitter.im/mobxjs/mobx

NPM

You need React version 16.8.0 and above

This is a lighter version of mobx-react which supports React functional components only and as such makes the library slightly faster and smaller (only 1.5kB gzipped). In fact mobx-react@6 has this library as a dependency and builds on top of it.

The library does not include any Provider/inject utilities as they can be fully replaced with React Context. Check out the migration guide.

Class based components are not supported except using <Observer> directly in class render method. If you want to transition existing projects from classes to hooks, use mobx-react 6+.

See more at the libraries overview.

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

The site contains various examples and recipes for using MobX in React world. Feel free to contribute. The API reference of this package follows 👇.

API reference âš’

<Observer>{renderFn}</Observer> (user guide)

Is a React component, which applies observer to an anonymous region in your component.

observer<P>(baseComponent: FunctionComponent<P>, options?: IObserverOptions): FunctionComponent<P> (user guide)

interface IObserverOptions {
  // Pass true to wrap the inner component with React.forwardRef.
  // It's false by the default.
  forwardRef?: boolean;
}

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.

useObserver<T>(fn: () => T, baseComponentName = "observed", options?: IUseObserverOptions): T (user guide)

interface IUseObserverOptions {
  // optional custom hook that should make a component re-render (or not) upon changes
  useForceUpdate: () => () => void;
}

It allows you to use an observer like behaviour, but still allowing you to optimize the component in any way you want (e.g. using memo with a custom areEqual, using forwardRef, etc.) and to declare exactly the part that is observed (the render phase).

useLocalStore<T, S>(initializer: () => T, source?: S): T (user guide)

Local observable state can be introduced by using the useLocalStore hook, that runs its initializer function once to create an observable store and keeps it around for a lifetime of a component.

useAsObservableSource<T>(source: T): T (user guide)

The useAsObservableSource hook can be used to turn any set of values into an observable object that has a stable reference (the same object is returned every time from the hook).

Optimize rendering

Check out the elaborate explanation.

If this is something that concerns you, we have prepared files you can simply import to configure MobX to use React batched updates depending on your platform.

React DOM:

import 'mobx-react-lite/optimizeForReactDom'

React Native:

import 'mobx-react-lite/optimizeForReactNative'

Import one of these before any React rendering is happening, typically index.js/ts. For Jest tests you can utilize setupFilesAfterEnv.

Custom batched updates

Above imports are for a convenience. If you for some reason have customized version of batched updates, you can do the following instead.

import { optimizeScheduler } from "mobx-react-lite";
optimizeScheduler(customBatchedUpdates);

Current Tags

  • 4.0.7-alpha                                ...           alpha (2 years ago)
  • 3.0.0-beta.1                                ...           beta (6 years ago)
  • 4.0.4-x1                                ...           experimental (3 years ago)
  • 5.1.2                                ...           latest (a month ago)
  • 2.0.0-alpha.5                                ...           next (7 years ago)

87 Versions

  • 5.1.2                                ...           a month ago
  • 5.1.1                                ...           a month ago
  • 5.1.0                                ...           2 months ago
  • 5.0.3                                ...           3 months ago
  • 5.0.2                                ...           3 months ago
  • 5.0.1                                ...           3 months ago
  • 5.0.0                                ...           4 months ago
  • 4.1.1                                ...           a year ago
  • 4.1.0                                ...           2 years ago
  • 4.0.7-alpha                                ...           2 years ago
  • 4.0.7                                ...           3 years ago
  • 4.0.6                                ...           3 years ago
  • 4.0.5                                ...           3 years ago
  • 4.0.4-x1                                ...           3 years ago
  • 4.0.4                                ...           3 years ago
  • 4.0.3                                ...           3 years ago
  • 4.0.2 [deprecated]           ...           4 years ago
  • 4.0.1 [deprecated]           ...           4 years ago
  • 3.4.3                                ...           4 years ago
  • 3.4.2                                ...           4 years ago
  • 3.4.1                                ...           4 years ago
  • 3.4.0                                ...           5 years ago
  • 3.3.0                                ...           5 years ago
  • 3.2.3                                ...           5 years ago
  • 3.2.2                                ...           5 years ago
  • 3.2.1                                ...           5 years ago
  • 3.2.0                                ...           6 years ago
  • 3.1.7                                ...           6 years ago
  • 3.1.6                                ...           6 years ago
  • 3.1.5                                ...           6 years ago
  • 3.1.4                                ...           6 years ago
  • 3.1.3                                ...           6 years ago
  • 3.1.2                                ...           6 years ago
  • 3.1.1                                ...           6 years ago
  • 3.1.0                                ...           6 years ago
  • 3.0.1                                ...           6 years ago
  • 3.0.0                                ...           6 years ago
  • 2.2.2                                ...           6 years ago
  • 2.2.1                                ...           6 years ago
  • 3.0.0-beta.1                                ...           6 years ago
  • 3.0.0-beta.0                                ...           6 years ago
  • 2.2.0                                ...           6 years ago
  • 2.2.0-beta.0                                ...           6 years ago
  • 2.1.0                                ...           6 years ago
  • 2.0.7                                ...           7 years ago
  • 2.0.6                                ...           7 years ago
  • 2.0.5                                ...           7 years ago
  • 2.0.4                                ...           7 years ago
  • 2.0.3                                ...           7 years ago
  • 2.0.2                                ...           7 years ago
  • 2.0.1                                ...           7 years ago
  • 2.0.0                                ...           7 years ago
  • 2.0.0-alpha.5                                ...           7 years ago
  • 1.5.2                                ...           7 years ago
  • 1.5.1                                ...           7 years ago
  • 2.0.0-alpha.4                                ...           7 years ago
  • 1.5.0                                ...           7 years ago
  • 2.0.0-alpha.3                                ...           7 years ago
  • 1.4.2                                ...           7 years ago
  • 1.4.1                                ...           7 years ago
  • 2.0.0-test.1                                ...           7 years ago
  • 2.0.0-test.0                                ...           7 years ago
  • 2.0.0-alpha.2                                ...           8 years ago
  • 1.4.0                                ...           8 years ago
  • 1.3.2                                ...           8 years ago
  • 1.3.1                                ...           8 years ago
  • 1.3.1-0                                ...           8 years ago
  • 2.0.0-alpha.1                                ...           8 years ago
  • 1.3.0                                ...           8 years ago
  • 2.0.0-alpha.0                                ...           8 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.1                                ...           8 years ago
  • 1.1.0                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
  • 0.3.7                                ...           8 years ago
  • 0.3.6                                ...           8 years ago
  • 0.3.5                                ...           8 years ago
  • 0.3.4                                ...           8 years ago
  • 0.3.3 [deprecated]           ...           8 years ago
  • 0.3.2                                ...           8 years ago
  • 0.3.1                                ...           8 years ago
  • 0.3.0                                ...           8 years ago
  • 0.2.0                                ...           8 years ago
  • 0.1.2                                ...           8 years ago
  • 0.1.1                                ...           8 years ago
  • 0.1.0                                ...           8 years ago
Maintainers (1)
Downloads
Total 283
Today 0
This Week 0
This Month 199
Last Day 0
Last Week 107
Last Month 2
Dependencies (0)
None
Dev Dependencies (27)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE