Created
May 14, 2020 09:04
-
-
Save zinozzino/53153c7f2d7880514807a8417cfb2bcb to your computer and use it in GitHub Desktop.
useDerivedState hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
useRef, | |
useEffect, | |
useState, | |
Dispatch, | |
SetStateAction, | |
} from 'react'; | |
const noop = () => { | |
return undefined; | |
}; | |
const isEqual = <T>(a: T, b: T) => a === b; | |
export function useDerivedState<T>( | |
prop: T, | |
onChange: (newProp: T) => void = noop, | |
predicate: (a: T, b: T) => boolean = isEqual | |
): [T, Dispatch<SetStateAction<T>>] { | |
const [state, setState] = useState(() => prop); | |
const prevProp = useRef(prop); | |
const ref = useRef(state); | |
const isPropChanged = !predicate(prevProp.current, prop); | |
const isStateChanged = !predicate(ref.current, state); | |
useEffect(() => { | |
ref.current = state; | |
if (!isPropChanged) { | |
onChange(state); | |
} | |
}, [isPropChanged, state]); | |
useEffect(() => { | |
if (!isStateChanged && isPropChanged) { | |
setState(prop); | |
} | |
prevProp.current = prop; | |
}, [isStateChanged, isPropChanged, prop]); | |
return [state, setState]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment