Skip to content

Commit

Permalink
Allows the force update to be infinitely caled
Browse files Browse the repository at this point in the history
This use case is far-fetched but in some contexts (gaming for example) the number of iterations can go beyond the maximum value supported by javascript and end up being `Infinity` which would cause the force update to stop working
because `Infinity + 1 === Infinity` is true in javascript

Using an object reference instead, we do not need to know how many force
render were done, just that it needs to rerender
  • Loading branch information
zeachco committed Jun 11, 2021
1 parent eb4fb5d commit 873a6a3
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions packages/mobx-react-lite/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { useCallback, useState } from "react"
const EMPTY_ARRAY: any[] = []

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

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

return update
}
Expand Down

0 comments on commit 873a6a3

Please sign in to comment.