Since upgrading to 4.18, it seems the ability to update graphic symbol properties is not working when using the Sketch widget.
If you checkout the codepen for the following Sketch widget sample and listen to update events, and then try to set the color of the graphic, it does not work.
The basic code I added to the codepen was this:
sketch.on("update", function(event) {
if (event.state === 'complete') {
event.graphics[0].symbol.set('color', [226, 119, 40]);
console.log(event.graphics[0])
}
});
This worked in 4.17, and can be proven in the above sample by editing to use ArcGIS JS version 4.17.
Solved! Go to Solution.
What is the goal of the above task? The code tries to change the symbol of the graphic, which at that time is a reference to the internal symbol used by the Sketch widget, so all subsequent graphics created via the Sketch widget will use the updated symbol. Is that what you want to do? If so, check out the 'polygonSymbol' property of the SketchViewModel as it might be an alternate approach. If the goal is to change the symbol for just the current graphic after it has been added to the layer, you'll can instead clone and update the symbol (so you don't change the Sketch internal symbol).
What is the goal of the above task? The code tries to change the symbol of the graphic, which at that time is a reference to the internal symbol used by the Sketch widget, so all subsequent graphics created via the Sketch widget will use the updated symbol. Is that what you want to do? If so, check out the 'polygonSymbol' property of the SketchViewModel as it might be an alternate approach. If the goal is to change the symbol for just the current graphic after it has been added to the layer, you'll can instead clone and update the symbol (so you don't change the Sketch internal symbol).
Thanks for the response, which I have accepted as a solution. However, please note that in previous versions it was possible to update a symbol color for the current graphic by using the approach shown in my original post, and it is only with v4.18 that this approach no longer works.
I'm not sure if this was intended change for v4.18 based on release documentation (..I could have overlooked something) and therefore I suspect this is a bug?
Hi there,
John's suggestion is spot on. You have to replace the entire symbol or clone it when you are updating the Graphic's symbol. It was working at 4.17 by "accident". In any case, I will update the Graphic.symbol document to indicate that needs to be cloned and reassigned to the graphic.
-Undral