Select to view content in your preferred language

Arcgis Map Component dismount - an avalanche of destruction!?

143
2
Jump to solution
Thursday
JonathanDawe_BAS
Regular Contributor

I’ve been using the <arcgis-map> component in a bunch of my React apps, and overall it works really well. But I’ve noticed something odd (and a bit frustrating) when the component is removed from the page.

Whenever the map component is unmounted or replaced, it calls the destroy method on the MapView. That makes sense on its own—but it also ends up destroying other ArcGIS API objects that I created separately, like Popup or FeatureLayer instances. Even though I created those outside of the component, they still get wiped out.

This has caused a few headaches. I often want to reuse things like a popup or a layer across multiple maps or views, kind of like singletons. But because the map component destroys them when it unmounts, I’m forced to recreate everything from scratch every time. That feels inefficient and makes my React code more complicated than it needs to be.

So I’m wondering:

  • Is this expected behaviour?

  • Is there a way to stop <arcgis-map> from destroying things I created outside of it?

  • Or is there a better pattern for reusing API instances safely?

Thanks in advance—keen to hear how others are handling this!

0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Esri Frequent Contributor

Great question, as this has come up a couple of times.

  • Yes, this is expected. Lots of apps will remove and add elements to the page at runtime, and custom elements have a built in lifecycle method to handle what to do when that happens. If we didn't destroy everything, this would lead memory leaks.
  • Every map-component has an autoDestroyDisabled property you can set to disable this behavior. This is useful to maintain maps and layers that might be used in other places in your application. Note, that this now puts the responsibility of calling the component destroy method on the developer when they are done to prevent memory leaks.
  • Not necessarily. In the next release it should be easier to hide components via css classes if you want to avoid adding/removing them.

View solution in original post

2 Replies
ReneRubalcava
Esri Frequent Contributor

Great question, as this has come up a couple of times.

  • Yes, this is expected. Lots of apps will remove and add elements to the page at runtime, and custom elements have a built in lifecycle method to handle what to do when that happens. If we didn't destroy everything, this would lead memory leaks.
  • Every map-component has an autoDestroyDisabled property you can set to disable this behavior. This is useful to maintain maps and layers that might be used in other places in your application. Note, that this now puts the responsibility of calling the component destroy method on the developer when they are done to prevent memory leaks.
  • Not necessarily. In the next release it should be easier to hide components via css classes if you want to avoid adding/removing them.
JonathanDawe_BAS
Regular Contributor

Thanks Rene - that will simplify things considerably! 

0 Kudos