How to remove listening scrolling events in react

it"s OK to listen for scrolling events in componentDidMount in the following code, but there is a remove listening event in componentWillUnmount when I jump to another page. Is there any way to solve it?
componentDidMount () {

    //
    window.addEventListener("scroll", () => {
        console.log("");
        const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
        const header = document.body.querySelector(".headerStyle")
        if (scrollTop > 100) {
            header.style.backgroundColor = "-sharpFF8949"
        } else if (scrollTop < 100 || scrollTop == 0) {
            header.style.backgroundColor = "rgba(0,0,0,0)"
        }
    })
}
componentWillUnmount() {
    //
    window.removeEventListener("scroll", () => {
        console.log("");
        const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
        const header = document.body.querySelector(".headerStyle")
        if (scrollTop > 100) {
            header.style.backgroundColor = "rgba(247, 247, 247, 1)"
        } else if (scrollTop < 100 || scrollTop == 0) {
            header.style.backgroundColor = "rgba(247, 247, 247, 1)"
        }
    })
}
Mar.15,2022

if you want to remove the event addEventListener , the executor must use an external function instead of an anonymous function as you do

const onScroll = () => {

};

//
window.addEventListener('scroll', onScroll);

//
window.removeEventListener('scroll', onScroll);

define a component method and then use it on the hook of the component life cycle.

{
    doSomething(){},
    componentDidMount(){
        window.addEventListener('scroll', this.doSomething)
    },
    componentWillUnmount(){
        window.removeEventListener('scroll', this.doSomething)
    }
}
Menu