mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
(nextjs) fixed image preloaders for SSR
This commit is contained in:
parent
7e14b52bf9
commit
d30cad9caa
4 changed files with 27 additions and 2 deletions
20
src/components/common/ImageWithSSRLoad/index.tsx
Normal file
20
src/components/common/ImageWithSSRLoad/index.tsx
Normal file
|
@ -0,0 +1,20 @@
|
|||
import React, { useEffect, useRef, VFC } from 'react';
|
||||
|
||||
interface ImgProps
|
||||
extends React.DetailedHTMLProps<React.ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement> {
|
||||
onLoad?: () => void;
|
||||
}
|
||||
|
||||
/** fixes the situation when img not fires onLoad at SSR */
|
||||
const ImageWithSSRLoad: VFC<ImgProps> = ({ onLoad, ...rest }) => {
|
||||
const ref = useRef<HTMLImageElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!ref.current?.complete || !onLoad) return;
|
||||
onLoad();
|
||||
}, [ref, onLoad]);
|
||||
|
||||
return <img {...rest} onLoad={onLoad} alt={rest.alt} ref={ref} />;
|
||||
};
|
||||
|
||||
export { ImageWithSSRLoad };
|
Loading…
Add table
Add a link
Reference in a new issue