-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
122 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"mobx-react": minor | ||
--- | ||
|
||
`observer(forwardRef(fn))` no longer generates extra `<Observer>` element and applies `memo` correctly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"mobx-react-lite": minor | ||
--- | ||
|
||
support `observable(forwardRef(fn))`, deprecate `observable(fn, { forwardRef: true })`, solve #2527, #3267 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,27 @@ | ||
import * as React from "react" | ||
import { observer as observerLite, Observer } from "mobx-react-lite" | ||
import { observer as observerLite } from "mobx-react-lite" | ||
|
||
import { makeClassComponentObserver } from "./observerClass" | ||
import { IReactComponent } from "./types/IReactComponent" | ||
|
||
const hasSymbol = typeof Symbol === "function" && Symbol.for | ||
|
||
// Using react-is had some issues (and operates on elements, not on types), see #608 / #609 | ||
const ReactForwardRefSymbol = hasSymbol | ||
? Symbol.for("react.forward_ref") | ||
: typeof React.forwardRef === "function" && React.forwardRef((props: any) => null)["$$typeof"] | ||
|
||
const ReactMemoSymbol = hasSymbol | ||
? Symbol.for("react.memo") | ||
: typeof React.memo === "function" && React.memo((props: any) => null)["$$typeof"] | ||
|
||
/** | ||
* Observer function / decorator | ||
*/ | ||
export function observer<T extends IReactComponent>(component: T): T { | ||
if (component["isMobxInjector"] === true) { | ||
console.warn( | ||
"Mobx observer: You are trying to use 'observer' on a component that already has 'inject'. Please apply 'observer' before applying 'inject'" | ||
"Mobx observer: You are trying to use `observer` on a component that already has `inject`. Please apply `observer` before applying `inject`" | ||
) | ||
} | ||
|
||
if (ReactMemoSymbol && component["$$typeof"] === ReactMemoSymbol) { | ||
throw new Error( | ||
"Mobx observer: You are trying to use 'observer' on a function component wrapped in either another observer or 'React.memo'. The observer already applies 'React.memo' for you." | ||
) | ||
} | ||
|
||
// Unwrap forward refs into `<Observer>` component | ||
// we need to unwrap the render, because it is the inner render that needs to be tracked, | ||
// not the ForwardRef HoC | ||
if (ReactForwardRefSymbol && component["$$typeof"] === ReactForwardRefSymbol) { | ||
const baseRender = component["render"] | ||
if (typeof baseRender !== "function") | ||
throw new Error("render property of ForwardRef was not a function") | ||
return React.forwardRef(function ObserverForwardRef() { | ||
const args = arguments | ||
return <Observer>{() => baseRender.apply(undefined, args)}</Observer> | ||
}) as T | ||
} | ||
|
||
// Function component | ||
if ( | ||
typeof component === "function" && | ||
(!component.prototype || !component.prototype.render) && | ||
!component["isReactClass"] && | ||
!Object.prototype.isPrototypeOf.call(React.Component, component) | ||
Object.prototype.isPrototypeOf.call(React.Component, component) || | ||
Object.prototype.isPrototypeOf.call(React.PureComponent, component) | ||
) { | ||
return observerLite(component as React.StatelessComponent<any>) as T | ||
// Class component | ||
return makeClassComponentObserver(component as React.ComponentClass<any, any>) as T | ||
} else { | ||
// Function component | ||
return observerLite(component as React.FunctionComponent<any>) as T | ||
} | ||
|
||
return makeClassComponentObserver(component as React.ComponentClass<any, any>) as T | ||
} |