Skip to content

Commit

Permalink
feat(unlock-app): using react-query to cache ENS resolution (#13889)
Browse files Browse the repository at this point in the history
* using react-query to cache ENS resolution

* caching forever
  • Loading branch information
julien51 committed May 24, 2024
1 parent 9d9457d commit 6e1da4f
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions unlock-app/src/hooks/useEns.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from 'ethers'
import { useState, useEffect } from 'react'
import configure from '../config'
import { useQuery } from '@tanstack/react-query'

const config = configure()

Expand Down Expand Up @@ -48,17 +48,14 @@ export const getAddressForName = async (_name: string): Promise<string> => {
* @param {*} address
*/
export const useEns = (address: string) => {
const [name, setName] = useState(address)

const getNameForAddress = async (_address: string) => {
setName(await getNameOrAddressForAddress(_address))
}

useEffect(() => {
getNameForAddress(address)
}, [address])

return name
const { data: name } = useQuery(
['ens', address],
async () => {
return getNameOrAddressForAddress(address)
},
{ staleTime: Infinity }
)
return name || address
}

export default useEns

0 comments on commit 6e1da4f

Please sign in to comment.