I have done it in React. I was trying something similar and come accross this thread. So in my case I wanted to remove an on click listener which would add some custom markers. Here is how I did it (for testing):
Attributes in File
const map = useRef(null);
map.current.on('click', (event) => addMarkerOnClick(event));
setTimeout(() => {
console.log("Disabled")
map.current.off('click', (event) => addMarkerOnClick(event));
}, 5000)
The above was how I had my code initially, it WONT work.
map.current.on('click', addMarkerOnClick);
setTimeout(() => {
console.log("Disabled")
map.current.off('click', addMarkerOnClick);
}, 5000)
That's the solution.
For none React users, I suggest this:
// Attributes
let map = null
map.on('click', addMarkerOnClick);
setTimeout(() => {
console.log("Disabled")
map.off('click', addMarkerOnClick);
}, 5000)