I am using React and ArcGIS to build an interactive map that will allow a User to:
The issue I am having is with the viewport, specifically 'zoom' and 'centre'. If, for example, somebody zooms in and then updates the data layers, it will revert back to the initial state viewport settings.
For reference, this is an example of a React interactive that I'm trying to achieve: https://www.fremantleports.com.au/the-port/port-map. As you can see, it allows you to update the data layers displayed (Buildings, Berths, etc...) without reverting back to the original zoom settings.
To achieve the above result, I have tried two methods:
useState and pass down the 'active layers' from the 'Sidebar Component' to the 'Map Component'. The problem is that it will revert back to the initial state viewport settings every time a user changes the data layers displayed.useContext (and also with react-tracked) and ArcGIS watchUtils functions to and keep track of the activeLayers, zoom, and centre using global state. However, the problem was that the map re-rendered every time the user changed the zoom level.https://www.fremantleports.com.au/the-port/port-map
Which of the above approach is preferable, and how should I use that approach to achieve the desired goal?
When the user performs an action that will change the map, the map must re-render.
I would say you should use useState to handle the state of the data layers and zoom level.
However, make sure you hold two separate state value, one for layers and one for the zoom level. Then, when you are rendering the map component, pass both of the states to the map component so it will render with the previous/new zoom level and layers. If the layer changed and the zoom level didn't, the previous level will be passed to map component and it won't initialize it.
Then, you can also use useMemo (Documentation) to save some unwanted renders (prevent re-render when value hasn't changed).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With