import { CSSProperties, useState } from 'react';
import styled from 'styled-components';
interface IProps {
src: string;
alt: string;
loadingComponent: JSX.Element;
style?: CSSProperties;
}
export default function ImageLoader({
src,
alt,
loadingComponent,
style,
}: IProps) {
const [isLoading, setIsLoading] = useState(true);
const handleLoad = () => {
setIsLoading(false);
};
return (
<>
{isLoading && (
<div
style={style}
>
{loadingComponent}
</div>
)}
<img
src={src}
alt={alt}
onLoad={handleLoad}
style={{...style, display: isLoading ? 'none' : 'block'}}
/>
</>
);
}
쉽게 이미지 로드 로딩을 만들어보았다.