【发布时间】:2023-03-19 23:14:01
【问题描述】:
我正在构建一个 react 应用程序(我是 javascript 的菜鸟),并且在组件卸载后尝试更新状态。
react 应用程序的上下文:作为学校项目的一部分,我必须为一个小型剧院综合体构建一个电影票预订服务,在网络应用程序的主页上,我有一个图像“轮播”,它正在更新图像每 5 秒一次。
这里是主页:https://apollo-9dd16.web.app/
(图像加载时间只是因为我选择了相当大的图像,将被修复) - 首次访问页面时的加载屏幕也是因为从firebase加载所有内容需要很长时间(所有内容都通过firebase数据库动态更新) ) 如果有办法让它更快,请告诉我????
另外,我浏览了尽可能多的 SO 帖子,每个人都建议在执行状态更改之前添加一个检查组件是否已安装,我想我已经完成了吗?
无论如何,关于这个问题,由于某种原因,它会说我有内存泄漏,因为我试图在卸载后更改组件状态(我无法轻松重现它,但它已经发生了一些次)
import React, {Component} from 'react'
import { Link } from 'react-router-dom'
import MoviesContext from '../contexts/Movies'
/* Todo:
- Work on the transition between movies
- Work on sending the user to the right place on book now button press
*/
class Home extends Component {
static contextType = MoviesContext
state = {
intervalID:0,
index:1,
prevIndex:0,
nextIndex:2,
picList:[]
}
componentDidMount() {
let mounted = true
const posterURLS = [] // eslint-disable-next-line
this.context.map((movies) => {
posterURLS.push(movies.posterURL)
})
this.setState({picList: posterURLS})
if(mounted) {
const newIntervalId = setInterval(() => {
if (this.state.nextIndex + 1 === this.state.picList.length ){
this.setState({
prevIndex: this.state.index,
index: this.state.nextIndex,
nextIndex: 0
})
} else {
this.setState({
prevIndex: this.state.index,
index: this.state.nextIndex,
nextIndex: this.state.nextIndex + 1
})
}
}, 5000);
this.setState(prevState => {
return {
...prevState,
intervalId: newIntervalId,
};
});
}
return () => mounted = false
}
render() {
return (
<div className="font-sans font-light text-theme-white text-4xl z-10 flex justify-center items-center h-screen">
<div className="h-3/4">
<div className="h-full justify-center items-center">
<div className="h-full hidden md:flex">
<img src={this.state.picList[this.state.prevIndex]} alt="this is a movie" className="h-full rounded-2xl -mx-20"/>
<img src={this.state.picList[this.state.nextIndex]} alt="this is a movie" className="h-full rounded-2xl -mx-20"/>
</div>
<div className="absolute inset-10 flex justify-center items-center h-screen">
<img src={this.state.picList[this.state.index]} alt="this is a movie" className="h-3/4 rounded-2xl"/>
</div>
</div>
<div className="absolute inset-0 top-10 flex justify-center items-center">
<Link to="/book" className="bg-theme-light text-theme-black rounded-2xl py-3 px-5 hover:bg-theme-black hover:text-theme-light">Book Now</Link>
</div>
</div>
</div>
)
}
}
export default Home
【问题讨论】:
标签: javascript reactjs jsx