Skip to content

Commit

Permalink
[Perf] Reduce useObserver gc pressure (#2995)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnaya committed Jun 20, 2021
1 parent 772a2f4 commit 10c762c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/reduce-useObserver-gc-pressure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx-react-lite": patch
---

Reduce useObserver gc pressure
6 changes: 5 additions & 1 deletion packages/mobx-react-lite/src/useObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ function observerComponentNameFor(baseComponentName: string) {
*/
class ObjectToBeRetainedByReact {}

function objectToBeRetainedByReactFactory() {
return new ObjectToBeRetainedByReact()
}

export function useObserver<T>(fn: () => T, baseComponentName: string = "observed"): T {
if (isUsingStaticRendering()) {
return fn()
}

const [objectRetainedByReact] = React.useState(new ObjectToBeRetainedByReact())
const [objectRetainedByReact] = React.useState(objectToBeRetainedByReactFactory)

const forceUpdate = useForceUpdate()

Expand Down

0 comments on commit 10c762c

Please sign in to comment.