I have used the following approach to change the style of my header on scroll which is working fine. Referred this answer
state = {
scrolled: false
};
componentDidMount() {
window.addEventListener('scroll', () => {
const isTop = window.scrollY < 84
if (!isTop) {
this.setState({
scrolled: true
})
} else {
this.setState({
scrolled: false
})
}
})
}
componentWillUnmount() {
window.removeEventListener('scroll')
}
The problem is that it works on every route(component) as I have the header component included in the App.js file but I want it to work specifically on the Home page only.
Is there a way to check the current route and perform this transition on header, only for the home component which will work with my current approach or do I need to change my approach altogether?