π‘ LazyLoad λ?
νμ΄μ§λ₯Ό μ½μ΄λ€μ΄λ μμ μ μ€μνμ§ μμ 리μμ€ λ‘λ©μ μΆ νμ νλ κΈ°μ λ‘ μ€ν¬λ‘€μ΄ λ΄λ €κ°λ©΄μ νμν λκ° λλ©΄ λ‘λκ° λμ΄μΌ ν©λλ€.
μλ₯Ό λ€μ΄, μ΄λ―Έμ§κ° 10μ₯μ΄ λλ νμ΄μ§κ° μλ€κ³ κ°μ ν λ μ΄λ―Έμ§λ₯Ό λͺ¨λ λ€ λ‘λκ° λ λκΉμ§ κΈ°λ€λ¦¬κ² λλ€λ©΄, νμ΄μ§μ λ‘λ©μ κΈΈμ΄μ§κ² λ κ²μ λλ€. νμ§λ§, 맨 μμ νλ©΄μ 보μ΄λ μ΄λ―Έμ§λ§ λ‘λλ₯Ό ν νμ, μ€ν¬λ‘€μ λ΄λ¦¬λ©΄μ μ΄λ―Έμ§κ° 보μ¬μ ΈμΌ ν λλ§λ€ μ΄λ―Έμ§λ₯Ό λ‘λνλ€λ©΄, λ°μ΄ν°μ λλΉλ₯Ό λ§μ μ μμ΅λλ€!
import React from "react";
import LazyLoad from "react-lazy-load";
export default function MyComponent() {
return (
<div>
<div className="filler" />
<LazyLoad height={600} offsetVertical={300}>
<img
src="https://cdn.pixabay.com/photo/2017/09/25/13/12/cocker-spaniel-2785074_1280.jpg"
width={500}
height={500}
/>
</LazyLoad>
<div className="filler" />
<LazyLoad height={600} offsetVertical={300}>
<img
src="/trick2.jpg"
width={500}
height={500}
/>
</LazyLoad>
<div className="filler" />
<LazyLoad height={600} offsetVertical={300}>
<img
src="/trick.jpg"
width={500}
height={500}
/>
</LazyLoad>
</div>
);
}
react-lazy-load λΌμ΄λΈλ¬λ¦¬λ₯Ό μ΄μ©ν΄λ³΄μλλ°μ. μ΄μ²λΌ νμ¬λ 보기 νΈνκΈ° μν΄μ 2κ°μ μ΄λ―Έμ§λ§ μ μμ§λ§ 10κ°λΌκ³ κ°μ νμλ μ€ν¬λ‘€μ λ΄λ €μΌ μ΄λ―Έμ§κ° λΆλ¬μμ§λλ€.
π‘ PreLoad λ?
νμ΄μ§λ₯Ό μ½μ΄λ€μΌ λ 미리 리μμ€λ₯Ό λ°μλλ κΈ°μ μ λλ€. μμ κ°μ΄ μλ₯Ό λ€μ΄ μ΄λ―Έμ§κ° 10μ₯μ΄ λλ νμ΄μ§κ° μλ€κ³ κ°μ ν©μλ€. LazyLoadμ κ²½μ°μλ νμν λλ§λ€ λ°μ΄ν°λ₯Ό λ‘λνλ λ°©λ²μ΄λΌλ©΄, PreLoadμ κ²½μ°μλ λͺ¨λ λ°μ΄ν°λ€μ 미리 λ‘λν΄λκ³ λκΈ°νλ λ°©μμ΄λΌ 보μλ©΄ λ©λλ€!
export default function ImagePreloadPage () {
const [imgTag, setImgTag] = useState()
const divRef = useRef(null)
useEffect(() => {
const img = new Image()
img.src = "λΆλ¬μ¬ μ΄λ―Έμ§ μ£Όμλ₯Ό λ£μ΅λλ€."
img.onload = () => {
setImgTag(img)
}
}, [])
const onClickPreload = () => {
if(imgTag) divRef.current?.appendChild(imgTag)
}
return (
<div>
<div ref={divRef}></div>
<button onClick={onClickPreload}> μ΄λ―Έμ§ ν리λ‘λ </button>
</div>
)
}