Skip to content

Commit

Permalink
forceupdate (#2983)
Browse files Browse the repository at this point in the history
  • Loading branch information
urugator committed Jun 20, 2021
1 parent c6dc925 commit 320544a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-rice-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx-react-lite": patch
---

Allow force update to be called infinitely times
7 changes: 3 additions & 4 deletions packages/mobx-react-lite/src/useObserver.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Reaction } from "mobx"
import React from "react"

import { printDebugValue } from "./utils/printDebugValue"
import {
addReactionToTrack,
IReactionTracking,
recordReactionAsCommitted
} from "./utils/reactionCleanupTracking"
import { isUsingStaticRendering } from "./staticRendering"
import { useForceUpdate } from "./utils/utils"

function observerComponentNameFor(baseComponentName: string) {
return `observer${baseComponentName}`
Expand All @@ -29,8 +27,9 @@ export function useObserver<T>(fn: () => T, baseComponentName: string = "observe
}

const [objectRetainedByReact] = React.useState(objectToBeRetainedByReactFactory)

const forceUpdate = useForceUpdate()
// Force update, see #2982
const [, setState] = React.useState()
const forceUpdate = () => setState([] as any)

// StrictMode/ConcurrentMode/Suspense may mean that our component is
// rendered and abandoned multiple times, so we need to track leaked
Expand Down
14 changes: 0 additions & 14 deletions packages/mobx-react-lite/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
import { useCallback, useState } from "react"

const EMPTY_ARRAY: any[] = []

export function useForceUpdate() {
const [, setTick] = useState(0)

const update = useCallback(() => {
setTick(tick => tick + 1)
}, EMPTY_ARRAY)

return update
}

const deprecatedMessages: string[] = []

export function useDeprecated(msg: string) {
Expand Down

0 comments on commit 320544a

Please sign in to comment.