uniswap-interface-uncensored/src/hooks/useGlobalChainSwitch.ts
Jordan Frankfurt b0b61f886d
fix: refactors the way chainId is accessed in some places (#4818)
fix: refactors the way some data is accessed
2022-10-06 11:55:46 -05:00

18 lines
720 B
TypeScript

import { useWeb3React } from '@web3-react/core'
import { Chain } from 'graphql/data/__generated__/TokenQuery.graphql'
import { chainIdToBackendName } from 'graphql/data/util'
import { useEffect, useRef } from 'react'
export const useOnGlobalChainSwitch = (callback: (chain: Chain) => void) => {
const { chainId: connectedChainId } = useWeb3React()
const globalChainName = chainIdToBackendName(connectedChainId)
const prevGlobalChainRef = useRef(globalChainName)
useEffect(() => {
if (prevGlobalChainRef.current !== globalChainName) {
callback(globalChainName)
}
prevGlobalChainRef.current = globalChainName
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [globalChainName])
}