Skip to content

Commit

Permalink
fix(react): flip boolean can assure react to force update
Browse files Browse the repository at this point in the history
  • Loading branch information
forehalo committed Sep 29, 2021
1 parent 40ec713 commit 10a1ca8
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/react/src/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EffectModule, ActionOfEffectModule } from '@sigi/core'
import { ConstructorOf, IStore } from '@sigi/types'
import { useEffect, useState, useRef, useMemo, useCallback } from 'react'
import { useEffect, useRef, useMemo, useCallback, useReducer } from 'react'
import { identity } from 'rxjs'
import { skip, tap } from 'rxjs/operators'

Expand All @@ -22,6 +22,12 @@ export function useDispatchers<M extends EffectModule<S>, S = any>(A: Constructo
return effectModule.dispatchers
}

function useForceUpdate() {
const [, forceRender] = useReducer((a) => a + 1, 0)

return forceRender
}

function _useModuleState<S, U = S>(
store: IStore<S>,
selector?: StateSelector<S, U>,
Expand All @@ -43,13 +49,13 @@ function _useModuleState<S, U = S>(
}
depsRef.current = dependencies

const [_, _flipSig] = useState(false)
const forceUpdate = useForceUpdate()

const tryUpdateState = useCallback((state: S) => {
const newState = selectorRef.current!(state)
if (!equalFn(stateRef.current, newState)) {
stateRef.current = newState
_flipSig((v) => !v)
forceUpdate()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
Expand Down

0 comments on commit 10a1ca8

Please sign in to comment.