I have a problem that I can not resolve in react : I want to change a background image each all 3 seconds. The code work to do this but it involves just one problem : when I update the "teaserAnimCount" state, I can see in my console that this value increase exponentially. I don't know why, with time, it's a problem because the web brother crash.
In the begining, the value of "console.log("this.state.teaserAnimCount") is 1 (it good), then 3, then 7, then 15,... If your are any idea why ?
This is in the renderTeaserBackground arrow function.
My code :
import React, { Component } from 'react';
import teaserImg1 from '../img/teaser-img-1.png';
import teaserImg2 from '../img/teaser-img-2.png';
import teaserImg3 from '../img/teaser-img-3.png';
import teaserImg4 from '../img/teaser-img-4.png';
import ProjetTitle from './ProjetTitle';
import { HashLink as Link } from 'react-router-hash-link';
import { TweenMax, Linear } from 'gsap';
import '../plugins/DrawSVGPlugin';
const teaserBgImg = [teaserImg1, teaserImg2, teaserImg3, teaserImg4];
class Teaser extends Component {
constructor(props) {
super(props);
this.state = {
teaserAnimDuration: 3,
teaserBg: teaserBgImg,
teaserAnimCount: 0,
teaserBgLength: teaserBgImg.length,
teaserBackground: ''
}
}
componentDidMount() {
TweenMax.from(
this.refs.circle,
this.state.teaserAnimDuration,
{
drawSVG: "0%",
ease: Linear.easeNone,
repeat: -1
}
)
}
renderTeaserBackground = () => {
setTimeout(function() {
let teaserBackground = this.state.teaserBg[this.state.teaserAnimCount];
this.setState({teaserAnimCount: this.state.teaserAnimCount + 1});
this.setState({teaserBackground});
console.log(this.state.teaserAnimCount);
}.bind(this), this.state.teaserAnimDuration * 1000);
return this.state.teaserBackground;
}
render() {
this.renderTeaserBackground();
let backgroundImg = {
backgroundImage: 'url(' + this.state.teaserBackground + ')'
}
return (
<Link to="/Content" className="teaser" style={backgroundImg}>
<svg className="svg-teaser" xmlns="http://www.w3.org/2000/svg">
<circle ref="circle" cx="50%" cy="50%" r="50%" fill="none"/>
</svg>
<div className="teaser-text-container flex">
</div>
<ProjetTitle className="teaser-info-teaser"/>
</Link>
);
}
}
export default Teaser;
Thank you for your help.