Make useStateCheckEquality
more flexible
This commit is contained in:
parent
1298f2ceb0
commit
0e4b317322
|
@ -1,5 +1,5 @@
|
||||||
import { isEqual } from 'lodash'
|
import { isEqual } from 'lodash'
|
||||||
import { useMemo, useRef, useState } from 'react'
|
import { SetStateAction, useMemo, useRef, useState } from 'react'
|
||||||
|
|
||||||
export const useStateCheckEquality = <T>(initialState: T) => {
|
export const useStateCheckEquality = <T>(initialState: T) => {
|
||||||
const [state, setState] = useState(initialState)
|
const [state, setState] = useState(initialState)
|
||||||
|
@ -8,8 +8,9 @@ export const useStateCheckEquality = <T>(initialState: T) => {
|
||||||
stateRef.current = state
|
stateRef.current = state
|
||||||
|
|
||||||
const checkSetState = useMemo(
|
const checkSetState = useMemo(
|
||||||
() => (newState: T) => {
|
() => (next: SetStateAction<T>) => {
|
||||||
const state = stateRef.current
|
const state = stateRef.current
|
||||||
|
const newState = next instanceof Function ? next(state) : next
|
||||||
if (!isEqual(state, newState)) {
|
if (!isEqual(state, newState)) {
|
||||||
setState(newState)
|
setState(newState)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user