I'm currently looping through a list of images with the purpose of animating the movement of a light on a bar.
(我目前正在遍历图像列表,以使灯光在酒吧上的运动动画化。)
Unfortunately I am not using CSS because it seems to me for my setup this looping would probably be the best way but it seems to eat a lot of resources so I am wondering if there is a way around this or if I simply have to change everything.(不幸的是,我没有使用CSS,因为在我的设置中,似乎这种循环可能是最好的方法,但是它似乎占用了很多资源,因此我想知道是否有解决方法,还是我只需要更改所有内容。)
Below is the code I am using:(以下是我正在使用的代码:)
Help on the code came from here:
(代码帮助来自此处:)
How to loop over images array and render them in a component for React?
(如何遍历图像数组并在React组件中渲染它们?)
Code
(码)
state = {
bars:[bar1,bar2,bar3,bar4,bar5],
activeImageIndex: 0
};
componentDidMount(){
setInterval(()=>{
let newActiveIndex = this.state.activeImageIndex===4 ? 0 : this.state.activeImageIndex+1
this.setState({
activeImageIndex: newActiveIndex
})
}, 1000);
}
<Image src={this.state.bars[activeImageIndex]} />
The code functions well for a bit then the app slows down as more resources seem to be taken.
(代码运行良好,然后由于似乎占用了更多资源而使应用速度变慢。)
Checking the network tool I can see resources growing (files are named differently in my app):(检查网络工具,我可以看到资源在增长(我的应用程序中文件的名称不同):)
Is there a better way to approach this?
(有没有更好的方法来解决这个问题?)
Or perhaps a way to free resources after each call?(还是在每次通话后释放资源的方法?)
ask by LoF10 translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…