Add useStateCheckEquality, and use for user & contract hooks
This commit is contained in:
parent
a2344492a2
commit
67d71fa531
|
@ -1,5 +1,6 @@
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { Contract, listenForContract } from '../lib/firebase/contracts'
|
import { Contract, listenForContract } from '../lib/firebase/contracts'
|
||||||
|
import { useStateCheckEquality } from './use-state-check-equality'
|
||||||
|
|
||||||
export const useContract = (contractId: string) => {
|
export const useContract = (contractId: string) => {
|
||||||
const [contract, setContract] = useState<Contract | null | 'loading'>(
|
const [contract, setContract] = useState<Contract | null | 'loading'>(
|
||||||
|
@ -14,12 +15,14 @@ export const useContract = (contractId: string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useContractWithPreload = (initial: Contract | null) => {
|
export const useContractWithPreload = (initial: Contract | null) => {
|
||||||
const [contract, setContract] = useState<Contract | null>(initial)
|
const [contract, setContract] = useStateCheckEquality<Contract | null>(
|
||||||
|
initial
|
||||||
|
)
|
||||||
const contractId = initial?.id
|
const contractId = initial?.id
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (contractId) return listenForContract(contractId, setContract)
|
if (contractId) return listenForContract(contractId, setContract)
|
||||||
}, [contractId])
|
}, [contractId, setContract])
|
||||||
|
|
||||||
return contract
|
return contract
|
||||||
}
|
}
|
||||||
|
|
21
web/hooks/use-state-check-equality.ts
Normal file
21
web/hooks/use-state-check-equality.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import _ from 'lodash'
|
||||||
|
import { 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(
|
||||||
|
() => (newState: T) => {
|
||||||
|
const state = stateRef.current
|
||||||
|
if (!_.isEqual(state, newState)) {
|
||||||
|
setState(newState)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[stateRef]
|
||||||
|
)
|
||||||
|
|
||||||
|
return [state, checkSetState] as const
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
import _ from 'lodash'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { PrivateUser } from '../../common/user'
|
import { PrivateUser } from '../../common/user'
|
||||||
import {
|
import {
|
||||||
|
@ -6,17 +7,20 @@ import {
|
||||||
listenForUser,
|
listenForUser,
|
||||||
User,
|
User,
|
||||||
} from '../lib/firebase/users'
|
} from '../lib/firebase/users'
|
||||||
|
import { useStateCheckEquality } from './use-state-check-equality'
|
||||||
|
|
||||||
export const useUser = () => {
|
export const useUser = () => {
|
||||||
const [user, setUser] = useState<User | null | undefined>(undefined)
|
const [user, setUser] = useStateCheckEquality<User | null | undefined>(
|
||||||
|
undefined
|
||||||
|
)
|
||||||
|
|
||||||
useEffect(() => listenForLogin(setUser), [])
|
useEffect(() => listenForLogin(setUser), [setUser])
|
||||||
|
|
||||||
const userId = user?.id
|
const userId = user?.id
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (userId) return listenForUser(userId, setUser)
|
if (userId) return listenForUser(userId, setUser)
|
||||||
}, [userId])
|
}, [userId, setUser])
|
||||||
|
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user