Last active
April 14, 2020 14:23
-
-
Save filipenevola/55245bbef4aaf8d15c283e892482479b to your computer and use it in GitHub Desktop.
Image fallback component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
<link rel="preload" href="/images/people-profile-default.png" as="fetch"> | |
</head> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
export const Image = ({ src, srcOnError, ...rest }) => { | |
const [srcImg, setSrcImg] = useState(null); | |
const onError = () => { | |
setSrcImg(srcOnError); | |
}; | |
const cachedSrc = transformUrlForCaching(src); | |
if (!srcOnError) { | |
return <img src={src} {...rest} />; | |
} | |
if (srcImg) { | |
return <img src={srcImg} {...rest} />; | |
} | |
return <img src={src} onError={onError} {...rest} />; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Image } from './Image'; | |
const DEFAULT_PROFILE_IMAGE_URL = '/images/people-profile-default.png'; | |
export const PersonImage = ({ profileImageUrl, ...rest }) => ( | |
<Image src={profileImageUrl} srcOnError={DEFAULT_PROFILE_IMAGE_URL} {...rest} /> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment