From 873a6a395d668e766c6f3b230bc248216a9c14e8 Mon Sep 17 00:00:00 2001 From: Olivier Rousseau-Guyot Date: Thu, 10 Jun 2021 15:53:42 -0400 Subject: [PATCH] Allows the force update to be infinitely caled 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 --- packages/mobx-react-lite/src/utils/utils.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/mobx-react-lite/src/utils/utils.ts b/packages/mobx-react-lite/src/utils/utils.ts index 22474ea73b..332d3ce180 100644 --- a/packages/mobx-react-lite/src/utils/utils.ts +++ b/packages/mobx-react-lite/src/utils/utils.ts @@ -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 }