Hi there,
I am using ScrollTrigger a lot, its a fantastic addition! the only issue I've run into so far is cleaning up when using ScrollTrigger.matchMedia() when un-mounting in my React components.
I've tried killing all ScrollTrigger instances, and killing timelines individually. simplified setup below:
const buildTimeline = () => {
// ... setup stuff
ScrollTrigger.saveStyles([boxRef.current, mobileRef.current]);
ScrollTrigger.matchMedia({
'(min-width: 720px)': () => {
if (!boxRef.current) {
console.log('boxRef does not exist');
}
ScrollTrigger.create({
// config stuff
animation: desktopTimeline.current
.to(
// animations
)
});
},
'(max-width: 719px)': () => {
if (!mobileRef.current) {
console.log('mobileRef does not exist');
}
ScrollTrigger.create({
// config stuff
animation: mobileTimeline.current
.to(
// animations
)
});
},
});
}
useEffect(() => {
if (!hasMounted.current) {
hasMounted.current = true;
buildTimeline();
}
return () => {
// kill all ScrollTrigger[s]
ScrollTrigger.getAll().forEach(t => t.kill());
// try killing individual timelines also
mobileTimeline.current.kill();
desktopTimeline.current.kill();
}
}, []);
This would normally work ok on ScrollTrigger instances generally - or at least it seems to! - but if I'm using matchMedia I'll still get media query change events firing where the component is unmounted (i.e. navigating to a different route). any idea what I'm missing here?