manifold/web/hooks/use-state-check-equality.ts
Marshall Polaris 3cb28cdecb
Teach AuthContext to manage the private user doc (#738)
* Return both user and privateUser from `createuser`

* Make `useStateCheckEquality` more flexible

* Make `AuthContext` track the private user doc

* Change `usePrivateUser` hook to use the auth context data

* Pass both user and private user through SSR to auth context

* Fix bug in create user flow
2022-08-12 13:41:00 -07:00

23 lines
600 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
}