Make useStateCheckEquality more flexible

This commit is contained in:
Marshall Polaris 2022-08-09 23:53:27 -07:00
parent 1298f2ceb0
commit 0e4b317322

View File

@ -1,5 +1,5 @@
import { isEqual } from 'lodash'
import { useMemo, useRef, useState } from 'react'
import { SetStateAction, useMemo, useRef, useState } from 'react'
export const useStateCheckEquality = <T>(initialState: T) => {
const [state, setState] = useState(initialState)
@ -8,8 +8,9 @@ export const useStateCheckEquality = <T>(initialState: T) => {
stateRef.current = state
const checkSetState = useMemo(
() => (newState: T) => {
() => (next: SetStateAction<T>) => {
const state = stateRef.current
const newState = next instanceof Function ? next(state) : next
if (!isEqual(state, newState)) {
setState(newState)
}