Files
Compass/web/hooks/use-state-check-equality.ts
Martin Braquet ba9b3cfb06 Add pretty formatting (#29)
* Test

* Add pretty formatting

* Fix Tests

* Fix Tests

* Fix Tests

* Fix

* Add pretty formatting fix

* Fix

* Test

* Fix tests

* Clean typeckech

* Add prettier check

* Fix api tsconfig

* Fix api tsconfig

* Fix tsconfig

* Fix

* Fix

* Prettier
2026-02-20 17:32:27 +01:00

23 lines
597 B
TypeScript

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