<?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: Trying to use a button to clear GraphicsLayer using GraphicsLayer.RemoveAll(). What am I doing wrong? in ArcGIS Experience Builder Questions</title>
    <link>https://community.esri.com/t5/arcgis-experience-builder-questions/trying-to-use-a-button-to-clear-graphicslayer/m-p/1598494#M18079</link>
    <description>&lt;P&gt;Thanks, that was indeed my issue. I had a feeling it might have something to do with how I was storing the layer, but I'm still new to using React so I'm still learning some of the concepts with it.&lt;/P&gt;&lt;P&gt;Much appreciated!&lt;/P&gt;</description>
    <pubDate>Mon, 24 Mar 2025 13:41:11 GMT</pubDate>
    <dc:creator>LClark1102</dc:creator>
    <dc:date>2025-03-24T13:41:11Z</dc:date>
    <item>
      <title>Trying to use a button to clear GraphicsLayer using GraphicsLayer.RemoveAll(). What am I doing wrong?</title>
      <link>https://community.esri.com/t5/arcgis-experience-builder-questions/trying-to-use-a-button-to-clear-graphicslayer/m-p/1597797#M18039</link>
      <description>&lt;P&gt;I'm trying to build a custom widget that gets the coordinates of a map when the mouse is clicked and adds it to a graphics layer called gLayer. In the return statement, I added a button with an onClick event that calls a function named clearResults. The clearResults function runs gLayer.removeAll(), which I think should clear all features that were previously added to gLayer. New features are added to the layer without issue, but when I click the button to clear the layer, nothing happens. Can anyone inspect my code and help me figure out what the issue is?&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;/** @jsx jsx */
import { React, WidgetState, type AllWidgetProps, jsx, getAppStore } from "jimu-core"
import { JimuMapViewComponent, type JimuMapView } from "jimu-arcgis"
import { Button } from 'jimu-ui'
import { SimpleLineSymbol, SimpleMarkerSymbol } from "esri/symbols"
import Point from 'esri/geometry/Point'
import Color from "esri/Color"
import Graphic from "esri/Graphic"
import GraphicsLayer from "esri/layers/GraphicsLayer"

const { useState } = React

export default function Widget (props:AllWidgetProps&amp;lt;any&amp;gt;) {

    const[latitude, setLatitude] = useState&amp;lt;string&amp;gt;('Click a point on the map. Coordinates will appear here.')
    const[longitude, setLongitude] = useState&amp;lt;string&amp;gt;('')
    
    var gLayer = new GraphicsLayer()
    
    const addGraphic = (latitude:number, longitude:number) =&amp;gt; {
        var point = new Point({
            latitude: latitude,
            longitude: longitude
        })
        var outline = new SimpleLineSymbol({
            color: new Color([255,255,255,0.85]),
            width: 1.5,
            style: "solid"
        })
        var pointSymbol = new SimpleMarkerSymbol({
            type: "simple-marker",
            style: "triangle",
            outline: outline,
            color: new Color([255,53,42,1000]),
            size: 18
        })
        var attributes = {
            Latitude: latitude.toFixed(5),
            Longitude: longitude.toFixed(5),
        }
        
        gLayer.add(new Graphic({
            geometry: point,
            symbol: pointSymbol,
            attributes: attributes,
            popupTemplate: {
                title: "Coordinates",
                content: [{
                    type: "fields",
                    fieldInfos: [{
                        fieldName: "Latitude",
                        label: "Latitude"
                    },
                    {
                        fieldName: "Longitude",
                        label: "Longitude"
                    }]
                }]
            }
        },
    )
        )
    }
    const activeViewChangeHandler = (jmv:JimuMapView) =&amp;gt; {
        var widgetState = WidgetState.Closed

        if(jmv) {
            jmv.view.map.add(gLayer)
            jmv.view.on('click', (evt) =&amp;gt; {
                widgetState = getAppStore().getState().widgetsRuntimeInfo[props.id].state

                if (widgetState === WidgetState.Opened) {
                    const point: Point = jmv.view.toMap({
                        x: evt.x,
                        y:evt.y
                    })
                    setLatitude("Latitude: " + point.latitude.toFixed(5))
                    setLongitude("Longitude: " + point.longitude.toFixed(5))
                    addGraphic(point.latitude, point.longitude)
                }
            })
        }
    }
    const clearResults = () =&amp;gt; {
        gLayer.removeAll()
    }

    return &amp;lt;div className="widget-starter jimu-widget" style={{ overflow: "auto"}}&amp;gt;
        {props.useMapWidgetIds &amp;amp;&amp;amp; props.useMapWidgetIds.length === 1 &amp;amp;&amp;amp; (
            &amp;lt;JimuMapViewComponent useMapWidgetId={props.useMapWidgetIds?.[0]} onActiveViewChange={activeViewChangeHandler} /&amp;gt;
        )}
        &amp;lt;p&amp;gt;{latitude}
        &amp;lt;br/&amp;gt;{longitude}&amp;lt;/p&amp;gt;
        &amp;lt;div&amp;gt;&amp;lt;Button onClick={clearResults} size="default"&amp;gt;Clear Results&amp;lt;/Button&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Mar 2025 21:09:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-experience-builder-questions/trying-to-use-a-button-to-clear-graphicslayer/m-p/1597797#M18039</guid>
      <dc:creator>LClark1102</dc:creator>
      <dc:date>2025-03-20T21:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use a button to clear GraphicsLayer using GraphicsLayer.RemoveAll(). What am I doing wrong?</title>
      <link>https://community.esri.com/t5/arcgis-experience-builder-questions/trying-to-use-a-button-to-clear-graphicslayer/m-p/1598373#M18072</link>
      <description>&lt;P&gt;You should use a ref instead of a plain variable to store the "gLayer".&lt;/P&gt;&lt;P&gt;const gLayer = useRef(new GraphicsLayer())&lt;/P&gt;&lt;P&gt;gLayer.current.add(...)&lt;/P&gt;&lt;P&gt;glayer.current.removeAll()&lt;/P&gt;&lt;P&gt;In React, a plain variable "gLayer" is a new one in every render.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Mar 2025 01:26:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-experience-builder-questions/trying-to-use-a-button-to-clear-graphicslayer/m-p/1598373#M18072</guid>
      <dc:creator>Allen_Zhang</dc:creator>
      <dc:date>2025-03-24T01:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use a button to clear GraphicsLayer using GraphicsLayer.RemoveAll(). What am I doing wrong?</title>
      <link>https://community.esri.com/t5/arcgis-experience-builder-questions/trying-to-use-a-button-to-clear-graphicslayer/m-p/1598494#M18079</link>
      <description>&lt;P&gt;Thanks, that was indeed my issue. I had a feeling it might have something to do with how I was storing the layer, but I'm still new to using React so I'm still learning some of the concepts with it.&lt;/P&gt;&lt;P&gt;Much appreciated!&lt;/P&gt;</description>
      <pubDate>Mon, 24 Mar 2025 13:41:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-experience-builder-questions/trying-to-use-a-button-to-clear-graphicslayer/m-p/1598494#M18079</guid>
      <dc:creator>LClark1102</dc:creator>
      <dc:date>2025-03-24T13:41:11Z</dc:date>
    </item>
  </channel>
</rss>

