Moh*_*lid 6 javascript reactjs next.js
出于搜索引擎优化和社交媒体链接的原因,我目前正在将 ReactJS 客户端呈现的应用程序转换为 NextJS 应用程序。
转换的组件之一,基本上是等待加载完成然后淡入的图像,在 NextJS 环境中使用后无法按预期工作。
它的行为方式如下:
缓存已启用:
使用 devtools 禁用缓存:
预期行为和之前仅使用 ReactJS 实现的行为:
当不使用 React 时,这个问题通常是因为有人src
在定义onload
函数之前设置了图像:
let img = new Image()
img.src = "img.jpg"
img.onload = () => console.log("Image loaded.")
Run Code Online (Sandbox Code Playgroud)
应该是:
let img = new Image()
img.onload = () => console.log("Image loaded.")
img.src = "img.jpg"
Run Code Online (Sandbox Code Playgroud)
这是在 NextJS 中导致相同问题的简化代码:
import React, { useState } from "react"
const Home = () => {
const [loaded, setLoaded] = useState(false)
const homeStyles = {
width: "100%",
height: "96vh",
backgroundColor: "black"
}
const imgStyles = {
width: "100%",
height: "100%",
objectFit: "cover",
opacity: loaded ? 1 : 0
}
const handleLoad = () => {
console.log("Loaded")
setLoaded(true)
}
return (
<div className="Home" style={homeStyles}>
<img alt=""
onLoad={handleLoad}
src="https://images.unsplash.com/photo-1558981001-5864b3250a69?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80"
style={imgStyles}
/>
</div>
)
}
export default Home
Run Code Online (Sandbox Code Playgroud)
Moh*_*lid 12
ImageObject.complete
由于有人的建议,我最终将其用作解决方法。
我曾经useRef
参考图像并检查image.current.complete === true
组件是否安装。
这是代码:
import React, { useEffect, useRef, useState } from "react"
const Home = () => {
const [loaded, setLoaded] = useState(false)
const image = useRef()
const homeStyles = {
width: "100%",
height: "96vh",
backgroundColor: "black"
}
const imgStyles = {
width: "100%",
height: "100%",
objectFit: "cover",
opacity: loaded ? 1 : 0
}
const handleLoad = () => setLoaded(true)
useEffect(() => {
if (image.current.complete) setLoaded(true)
}, [])
return (
<div className="Home" style={homeStyles}>
<img alt=""
ref={image}
onLoad={handleLoad}
src="https://images.unsplash.com/photo-1558981001-5864b3250a69?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80"
style={imgStyles}
/>
</div>
)
}
export default Home
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4573 次 |
最近记录: |