Skip to content
This repository was archived by the owner on Dec 31, 2020. It is now read-only.

Commit 175fee8

Browse files
authored
Revert "[Experimental][WIP] Refactor api (#306)" (#310)
This reverts commit 916fd55.
1 parent 916fd55 commit 175fee8

28 files changed

+556
-1159
lines changed

.github/ISSUE_TEMPLATE/documentation.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ labels: documentation
66

77
Basic API documentation lives in the `README.md` file. Please, send in PR directly with a change instead of describing what you want to change.
88

9-
Typical user patterns are maintained at https://github.com/mobxjs/mobx/blob/mobx6/docs/react/react-integration.md
10-
119
More up to date information can be found at https://mobx-react.js.org/ site so feel free to contribute there.
1210

1311
Ask here only if your change is bigger and there is a chance for rejecting it.

.watchmanconfig

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 23 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,122 +8,58 @@
88

99
[![NPM](https://nodei.co/npm/mobx-react-lite.png)](https://www.npmjs.com/package/mobx-react-lite)
1010

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**
1412

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.
1614

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).
2216

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).
2418

25-
## User Guide 👉 https://mobx.js.org/react/react-integration.html
19+
See more at [the libraries overview](https://mobx-react.js.org/libraries).
2620

27-
---
21+
## User Guide 👉 https://mobx-react.js.org
2822

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 👇.
3924

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 ⚒
4926

50-
### **`useStaticRendering(enable: true)`**
27+
### **`<Observer>{renderFn}</Observer>`** _([user guide](https://mobx-react.js.org/observer-component))_
5128

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.
5330

54-
---
31+
### **`observer<P>(baseComponent: FunctionComponent<P>, options?: IObserverOptions): FunctionComponent<P>`** _([user guide](https://mobx-react.js.org/observer-hoc))_
5532

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+
```
5740

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.
5942

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))_
6144

6245
```ts
6346
interface IUseObserverOptions {
6447
// optional custom hook that should make a component re-render (or not) upon changes
65-
// Supported in 2.x only
6648
useForceUpdate: () => () => void
6749
}
6850
```
6951

7052
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).
7153

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))_
7555

7656
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.
7757

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))_
8159

8260
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).
8361

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
12763

12864
_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_
12965

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
"bugs": {
4343
"url": "https://github.com/mobxjs/mobx-react-lite/issues"
4444
},
45-
"homepage": "https://mobx.js.org",
45+
"homepage": "https://mobx-react.js.org",
4646
"peerDependencies": {
47-
"mobx": "^6.0.0",
47+
"mobx": "^4.0.0 || ^5.0.0",
4848
"react": "^16.8.0"
4949
},
5050
"peerDependenciesMeta": {
@@ -76,7 +76,7 @@
7676
"jest": "24.9.0",
7777
"jest-environment-jsdom": "24.9.0",
7878
"jest-mock-console": "1.0.0",
79-
"mobx": "^6.0.0-rc.7",
79+
"mobx": "5.14.0",
8080
"prettier": "^1.19.1",
8181
"pretty-quick": "^2.0.1",
8282
"prompts": "^2.3.2",
@@ -110,4 +110,4 @@
110110
}
111111
},
112112
"dependencies": {}
113-
}
113+
}

src/ObserverComponent.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ function ObserverComponent({ children, render }: IObserverProps) {
1212
}
1313
return useObserver(component)
1414
}
15-
if ("production" !== process.env.NODE_ENV) {
16-
ObserverComponent.propTypes = {
17-
children: ObserverPropsCheck,
18-
render: ObserverPropsCheck
19-
}
15+
ObserverComponent.propTypes = {
16+
children: ObserverPropsCheck,
17+
render: ObserverPropsCheck
2018
}
2119
ObserverComponent.displayName = "Observer"
2220

src/assertEnvironment.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { spy } from "mobx"
2+
import { useState } from "react"
3+
4+
if (!useState) {
5+
throw new Error("mobx-react-lite requires React with Hooks support")
6+
}
7+
if (!spy) {
8+
throw new Error("mobx-react-lite requires mobx at least version 4 to be available")
9+
}

src/index.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
import "./utils/assertEnvironment"
1+
import "./assertEnvironment"
22

33
import { unstable_batchedUpdates as batch } from "./utils/reactBatchedUpdates"
4-
import { observerBatching } from "./utils/observerBatching"
5-
import { useDeprecated } from "./utils/utils"
6-
import { useObserver as useObserverOriginal } from "./useObserver"
4+
import { observerBatching } from "./observerBatching"
75

86
observerBatching(batch)
97

108
export { isUsingStaticRendering, useStaticRendering } from "./staticRendering"
119
export { observer, IObserverOptions } from "./observer"
10+
export { useObserver, ForceUpdateHook, IUseObserverOptions } from "./useObserver"
1211
export { Observer } from "./ObserverComponent"
13-
export { useLocalObservable } from "./useLocalObservable"
14-
export { observerBatching } from "./utils/observerBatching"
15-
export { useLocalStore } from "./useLocalStore"
12+
export { useForceUpdate } from "./utils"
1613
export { useAsObservableSource } from "./useAsObservableSource"
17-
18-
export function useObserver<T>(fn: () => T, baseComponentName: string = "observed"): T {
19-
if ("production" !== process.env.NODE_ENV) {
20-
useDeprecated(
21-
"[mobx-react-lite] 'useObserver(fn)' is deprecated. Use `<Observer>{fn}</Observer>` instead, or wrap the entire component in `observer`."
22-
)
23-
}
24-
return useObserverOriginal(fn, baseComponentName)
25-
}
14+
export { useLocalStore } from "./useLocalStore"
15+
export { useQueuedForceUpdate, useQueuedForceUpdateBlock } from "./useQueuedForceUpdate"
16+
export { observerBatching } from "./observerBatching"
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)