<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: React: Basemap Toggle Viewmodel mutates its input props in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/react-basemap-toggle-viewmodel-mutates-its-input/m-p/1346211#M82700</link>
    <description>&lt;P&gt;Thanks Addison - I made a slight change which was to not pass in the props into the new BasemapToggleVm function directly which looked to resolve the issue.&amp;nbsp;Here is a working sample:&amp;nbsp;&lt;A href="https://codesandbox.io/p/sandbox/esri-infinite-hook-forked-nrkp39?file=%2Fsrc%2FBasemapToggleButton.tsx%3A1%2C1" target="_blank" rel="noopener"&gt;https://codesandbox.io/p/sandbox/esri-infinite-hook-forked-nrkp39?file=%2Fsrc%2FBasemapToggleButton.tsx%3A1%2C1&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 06 Nov 2023 17:30:40 GMT</pubDate>
    <dc:creator>JonnyDawe</dc:creator>
    <dc:date>2023-11-06T17:30:40Z</dc:date>
    <item>
      <title>React: Basemap Toggle Viewmodel mutates its input props</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/react-basemap-toggle-viewmodel-mutates-its-input/m-p/1339933#M82519</link>
      <description>&lt;P&gt;I have created a custom React hook so that I can utilise the Basemap toggle view model logic in another section of my application. I am following a pattern similar to that which is described in this repo:&amp;nbsp;&lt;A href="https://github.com/rslibed/2023DS-build-a-custom-ui-for-api-widgets-bookmarks/tree/master/src/Components/Bookmarks" target="_blank"&gt;https://github.com/rslibed/2023DS-build-a-custom-ui-for-api-widgets-bookmarks/tree/master/src/Components/Bookmarks&lt;/A&gt;&lt;/P&gt;&lt;P&gt;The issue I am seeing is that the hook I create infinitely re-renders because the view model mutates the properties it takes in. In this case I specifically see the nextBasemap property is being altered when I call new BaseMapToggleVM.&lt;/P&gt;&lt;P&gt;The only fix I can see is that I clone some of these objects within the custom hook, so that I don't directly pass them into the viewmodel, but it really surprised me that the esri logic would be so gungho about directly mutating the values I passed in. In fact it seems to break a very standard ESlint rule:&amp;nbsp;&lt;A href="https://eslint.org/docs/latest/rules/no-param-reassign" target="_blank"&gt;https://eslint.org/docs/latest/rules/no-param-reassign&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Am I missing something here? Is this a bug or just something I need to deal with/work around?&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import BasemapToggleVM from "@arcgis/core/widgets/BasemapToggle/BasemapToggleViewModel";
import React from "react";
import Handles from "@arcgis/core/core/Handles";
import { watch } from "@arcgis/core/core/reactiveUtils";


export function useBaseMapToggleModel({ ...props }: __esri.BasemapToggleViewModelProperties) {
const [basemapToggleVM, setBasemapToggleVm] =
React.useState&amp;lt;__esri.BasemapToggleViewModel | null&amp;gt;(null);


const [state, setState] = React.useState&amp;lt;BasemapToggleVM["state"]&amp;gt;("disabled");


React.useEffect(() =&amp;gt; {
const basemapToggleModel = new BasemapToggleVM(props);
setBasemapToggleVm(basemapToggleModel);


return () =&amp;gt; {
basemapToggleModel.destroy();
};
}, [props]);


React.useEffect(() =&amp;gt; {
if (!basemapToggleVM) return () =&amp;gt; {};


const handles = new Handles();


addHandlers({ basemapToggleVM, handles, onStateChange: setState });


return () =&amp;gt; {
handles.removeAll();
handles.destroy();
};
}, [basemapToggleVM]);


return {
state,
toggle: () =&amp;gt; {
basemapToggleVM?.toggle();
}
};
}


interface HandlerSetup {
basemapToggleVM: __esri.BasemapToggleViewModel;
handles: Handles;
onStateChange: (state: BasemapToggleVM["state"]) =&amp;gt; void;
}
function addHandlers({ basemapToggleVM, handles, onStateChange }: HandlerSetup) {
handles.removeAll();


handles.add([
watch(
() =&amp;gt; basemapToggleVM.state,
(state: BasemapToggleVM["state"]) =&amp;gt; onStateChange?.call(null, state),
{ initial: true }
)
]);
}
 &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 08:09:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/react-basemap-toggle-viewmodel-mutates-its-input/m-p/1339933#M82519</guid>
      <dc:creator>JonnyDawe</dc:creator>
      <dc:date>2023-10-20T08:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: React: Basemap Toggle Viewmodel mutates its input props</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/react-basemap-toggle-viewmodel-mutates-its-input/m-p/1343154#M82612</link>
      <description>&lt;P&gt;Objects in effect dependencies are compared with Object.is(). Without taking a closer look at your example, I’d bet you aren’t memoizing the arguments before they are passed to your hook. Having arguments to a hook at all is kind of a dangerous pattern, you can safeguard in a hacky way by comparing my incoming props to a useReffed value of them with something like fast-deep-equals. If they are comparatively different, then you can mutate your hooks inner state and kick off subsequent effects.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 03:05:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/react-basemap-toggle-viewmodel-mutates-its-input/m-p/1343154#M82612</guid>
      <dc:creator>AddisonShaw</dc:creator>
      <dc:date>2023-10-30T03:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: React: Basemap Toggle Viewmodel mutates its input props</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/react-basemap-toggle-viewmodel-mutates-its-input/m-p/1346211#M82700</link>
      <description>&lt;P&gt;Thanks Addison - I made a slight change which was to not pass in the props into the new BasemapToggleVm function directly which looked to resolve the issue.&amp;nbsp;Here is a working sample:&amp;nbsp;&lt;A href="https://codesandbox.io/p/sandbox/esri-infinite-hook-forked-nrkp39?file=%2Fsrc%2FBasemapToggleButton.tsx%3A1%2C1" target="_blank" rel="noopener"&gt;https://codesandbox.io/p/sandbox/esri-infinite-hook-forked-nrkp39?file=%2Fsrc%2FBasemapToggleButton.tsx%3A1%2C1&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 17:30:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/react-basemap-toggle-viewmodel-mutates-its-input/m-p/1346211#M82700</guid>
      <dc:creator>JonnyDawe</dc:creator>
      <dc:date>2023-11-06T17:30:40Z</dc:date>
    </item>
  </channel>
</rss>

