Commit 709f0299 authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: remove orphaned node (#2863)

* fix: remove orphaned node

* fix: react cleanup
parent 6b57ffe3
import jazzicon from '@metamask/jazzicon' import jazzicon from '@metamask/jazzicon'
import useENSAvatar from 'hooks/useENSAvatar' import useENSAvatar from 'hooks/useENSAvatar'
import { useEffect, useRef, useState } from 'react' import { useCallback, useState } from 'react'
import styled from 'styled-components/macro' import styled from 'styled-components/macro'
import { useActiveWeb3React } from '../../hooks/web3' import { useActiveWeb3React } from '../../hooks/web3'
...@@ -20,27 +20,28 @@ const StyledAvatar = styled.img` ...@@ -20,27 +20,28 @@ const StyledAvatar = styled.img`
` `
export default function Identicon() { export default function Identicon() {
const ref = useRef<HTMLDivElement>(null)
const { account } = useActiveWeb3React() const { account } = useActiveWeb3React()
const { avatar } = useENSAvatar(account ?? undefined) const { avatar } = useENSAvatar(account ?? undefined)
const [fetchable, setFetchable] = useState(true) const [fetchable, setFetchable] = useState(true)
useEffect(() => { const setJazzicon = useCallback(
if ((!avatar || !fetchable) && account) { (ref: HTMLSpanElement | null) => {
const icon = jazzicon(16, parseInt(account?.slice(2, 10), 16)) if (account) {
const current = ref.current ref?.appendChild(jazzicon(16, parseInt(account?.slice(2, 10), 16)))
current?.appendChild(icon)
return () => {
current?.removeChild(icon)
} }
} },
return [account]
}, [account, avatar, fetchable]) )
return ( return (
<StyledIdenticon ref={ref}> <StyledIdenticon>
{avatar && fetchable && ( {avatar && fetchable ? (
<StyledAvatar alt="avatar" src={avatar} onError={() => setFetchable(false)}></StyledAvatar> <StyledAvatar alt="avatar" src={avatar} onError={() => setFetchable(false)}></StyledAvatar>
) : (
<span
ref={setJazzicon}
key={account} // forces re-render when account changes so that React controls jazzicon cleanup
/>
)} )}
</StyledIdenticon> </StyledIdenticon>
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment