Skip to content
Advertisement

Functional component event is still active when state is toggled – REACT MAPBOX

I’m trying to build an app where you can toggle effects by clicking on the navitems.

On the nav items I have an event listener that toggles state

JavaScript

The idea is when it’s set to true, you can click on the map to set a waypoint. And when it’s false, clicking does the default behavior.

I tried using an if statement like this

JavaScript

Whether state is true or false, you can still place a waypoint after toggling it for the first time.

Can someone please tell me what I’m missing here? I know it’s something stupid.

Advertisement

Answer

I’ve faced the same problem when I began to use React Hooks! Don’t worry, that’s because you have no code for removing the added click event for map Ref! So you just need to cleanup the click event for the map object!

JavaScript

Please try this way.. Good luck..

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement