I was just going through the code of react-slick and came across the following peice of code:-
this.callbackTimers.push(
setTimeout(() => this.setState({ animating }), 10)
);
In the componentWillUnmount the callbackTimers are cleared like so :-
componentWillUnmount = () => {
if (this.callbackTimers.length) {
this.callbackTimers.forEach(timer => clearTimeout(timer));
this.callbackTimers = [];
}
};
Is the sole purpose of using the array to free memory or is there something that i have missed here ?
Why not just call the setTimeout directly:
setTimeout(() => this.setState({ animating }), 10)
instead of using an array ? i do see callbackTimers
being used elsewhere too , but i don't know why exactly this array is needed apart from freeing memory, Is there any other purpose to this array ?
The line in question can be found HERE.