|
8 | 8 |
|
9 | 9 | [](https://www.npmjs.com/package/mobx-react-lite)
|
10 | 10 |
|
11 |
| -This is a lighter version of [mobx-react](https://github.com/mobxjs/mobx-react) which supports React **functional components only** and as such makes the library slightly faster and smaller (_only 1.5kB gzipped_). Note however that it is possible to use `<Observer>` inside the render of class components. |
12 |
| -Unlike `mobx-react`, it doesn't `Provider`/`inject`, as `useContext` can be used instead. |
13 |
| -It also doesn't offer |
| 11 | +**You need React version 16.8.0 and above** |
14 | 12 |
|
15 |
| -## Compatibility table (major versions) |
| 13 | +This is a lighter version of [mobx-react](https://github.com/mobxjs/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. |
16 | 14 |
|
17 |
| -| mobx | mobx-react-lite | Browser | |
18 |
| -| ---- | --------------- | ---------------------------------------------- | |
19 |
| -| 6 | 3 | Modern browsers (IE 11+ in compatibility mode) | |
20 |
| -| 5 | 2 | Modern browsers | |
21 |
| -| 4 | 2 | IE 11+, RN w/o Proxy support | |
| 15 | +The library does not include any Provider/inject utilities as they can be fully replaced with [React Context](https://mobx-react.js.org/recipes-context). Check out [the migration guide](https://mobx-react.js.org/recipes-migration). |
22 | 16 |
|
23 |
| -`mobx-react-lite` requires React 16.8 or higher. |
| 17 | +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+](https://github.com/mobxjs/mobx-react). |
24 | 18 |
|
25 |
| -## User Guide 👉 https://mobx.js.org/react/react-integration.html |
| 19 | +See more at [the libraries overview](https://mobx-react.js.org/libraries). |
26 | 20 |
|
27 |
| ---- |
| 21 | +## User Guide 👉 https://mobx-react.js.org |
28 | 22 |
|
29 |
| -## API reference ⚒ |
30 |
| - |
31 |
| -### **`observer<P>(baseComponent: FunctionComponent<P>): FunctionComponent<P>`** |
32 |
| - |
33 |
| -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. |
34 |
| -Can only be used for function components. For class component support see the `mobx-react` package. |
35 |
| - |
36 |
| -### **`<Observer>{renderFn}</Observer>`** |
37 |
| - |
38 |
| -Is a React component, which applies observer to an anonymous region in your component. `<Observer>` can be used both inside class and function components. |
| 23 | +The site contains various examples and recipes for using MobX in React world. Feel free to contribute. The API reference of this package follows 👇. |
39 | 24 |
|
40 |
| -### **`useLocalObservable<T>(initializer: () => T, annotations?: AnnotationsMap<T>): T`** |
41 |
| - |
42 |
| -Creates an observable object with the given properties, methods and computed values. |
43 |
| - |
44 |
| -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` (see the example below at `useAsObservableSource`). |
45 |
| - |
46 |
| -`useLocalObservable` is a short-hand for: |
47 |
| - |
48 |
| -`const [state] = useState(() => observable(initializer(), annotations, { autoBind: true }))` |
| 25 | +## API reference ⚒ |
49 | 26 |
|
50 |
| -### **`useStaticRendering(enable: true)`** |
| 27 | +### **`<Observer>{renderFn}</Observer>`** _([user guide](https://mobx-react.js.org/observer-component))_ |
51 | 28 |
|
52 |
| -Call `useStaticRendering(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. |
| 29 | +Is a React component, which applies observer to an anonymous region in your component. |
53 | 30 |
|
54 |
| ---- |
| 31 | +### **`observer<P>(baseComponent: FunctionComponent<P>, options?: IObserverOptions): FunctionComponent<P>`** _([user guide](https://mobx-react.js.org/observer-hoc))_ |
55 | 32 |
|
56 |
| -## Deprecated APIs |
| 33 | +```ts |
| 34 | +interface IObserverOptions { |
| 35 | + // Pass true to wrap the inner component with React.forwardRef. |
| 36 | + // It's false by the default. |
| 37 | + forwardRef?: boolean |
| 38 | +} |
| 39 | +``` |
57 | 40 |
|
58 |
| -### **`useObserver<T>(fn: () => T, baseComponentName = "observed", options?: IUseObserverOptions): T`** (deprecated) |
| 41 | +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. |
59 | 42 |
|
60 |
| -_This API is deprecated in 3.\*. It is often used wrong (e.g. to select data rather than for rendering, and `<Observer>` better decouples the rendering from the component updates_ |
| 43 | +### **`useObserver<T>(fn: () => T, baseComponentName = "observed", options?: IUseObserverOptions): T`** _([user guide](https://mobx-react.js.org/observer-hook))_ |
61 | 44 |
|
62 | 45 | ```ts
|
63 | 46 | interface IUseObserverOptions {
|
64 | 47 | // optional custom hook that should make a component re-render (or not) upon changes
|
65 |
| - // Supported in 2.x only |
66 | 48 | useForceUpdate: () => () => void
|
67 | 49 | }
|
68 | 50 | ```
|
69 | 51 |
|
70 | 52 | 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).
|
71 | 53 |
|
72 |
| -### **`useLocalStore<T, S>(initializer: () => T, source?: S): T`** (deprecated) |
73 |
| - |
74 |
| -_This API is deprecated in 3.\*. Use `useLocalObservable` instead. They do roughly the same, but `useLocalObservable` accepts an set of annotations as second argument, rather than a `source` object. Using `source` is not recommended, see the deprecation message at `useAsObservableSource` for details_ |
| 54 | +### **`useLocalStore<T, S>(initializer: () => T, source?: S): T`** _([user guide](https://mobx-react.js.org/state-local))_ |
75 | 55 |
|
76 | 56 | 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.
|
77 | 57 |
|
78 |
| -The annotations are similar to the annotations that are passed in to MobX's [`observable`](https://mobx.js.org/observable.html#available-annotations) API, and can be used to override the automatic member inference of specific fields. |
79 |
| - |
80 |
| -### **`useAsObservableSource<T>(source: T): T`** (deprecated) |
| 58 | +### **`useAsObservableSource<T>(source: T): T`** _([user guide](https://mobx-react.js.org/state-outsourcing))_ |
81 | 59 |
|
82 | 60 | 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).
|
83 | 61 |
|
84 |
| -_This API is deprecated in 3.\* as it relies on observables to be updated during rendering which is an anti-pattern. Instead, use `useEffect` to synchronize non-observable values with values. Example:_ |
85 |
| - |
86 |
| -```javascript |
87 |
| -// Before: |
88 |
| -function Measurement({ unit }) { |
89 |
| - const observableProps = useAsObservableSource({ unit }) |
90 |
| - const state = useLocalStore(() => ({ |
91 |
| - length: 0, |
92 |
| - get lengthWithUnit() { |
93 |
| - // lengthWithUnit can only depend on observables, hence the above conversion with `useAsObservableSource` |
94 |
| - return observableProps.unit === "inch" |
95 |
| - ? `${this.length * 2.54} inch` |
96 |
| - : `${this.length} cm` |
97 |
| - } |
98 |
| - })) |
99 |
| - |
100 |
| - return <h1>{state.lengthWithUnit}</h1> |
101 |
| -} |
102 |
| - |
103 |
| -// After: |
104 |
| -function Measurement({ unit }) { |
105 |
| - const state = useLocalObservable(() => ({ |
106 |
| - unit, // the initial unit |
107 |
| - length: 0, |
108 |
| - get lengthWithUnit() { |
109 |
| - // lengthWithUnit can only depend on observables, hence the above conversion with `useAsObservableSource` |
110 |
| - return this.unit === "inch" ? `${this.length * 2.54} inch` : `${this.length} cm` |
111 |
| - } |
112 |
| - })) |
113 |
| - |
114 |
| - useEffect(() => { |
115 |
| - // sync the unit from 'props' into the observable 'state' |
116 |
| - state.unit = unit |
117 |
| - }, [unit]) |
118 |
| - |
119 |
| - return <h1>{state.lengthWithUnit}</h1> |
120 |
| -} |
121 |
| -``` |
122 |
| - |
123 |
| -Note that, at your own risk, it is also possible to not use `useEffect`, but do `state.unit = unit` instead in the rendering. |
124 |
| -This is closer to the old behavior, but React will warn correctly about this if this would affect the rendering of other components. |
125 |
| - |
126 |
| -## Observer batching (deprecated) |
| 62 | +## Observer batching |
127 | 63 |
|
128 | 64 | _Note: configuring observer batching is only needed when using `mobx-react-lite` 2.0.* or 2.1.*. From 2.2 onward it will be configured automatically based on the availability of react-dom / react-native packages_
|
129 | 65 |
|
|
0 commit comments