Is it possible and how to render list each time when an array is updated? For example I have this layers button which render list of all layers in active document, but I don’t know how to automatically update list each time when I add or remove some layer, without clicking. I tried some options with useState and useEffect but obviously I don’t know how to use them properly.
if you want to make plugin which detects current layer automatically,
I think you need to use event on Photoshop.
setting layer event like below the code.
const { app } = require("photoshop");
const photoshop = require("photoshop");
const listener = (e) => {
console.log(e);// param is event name.
console.log([...app.activeDocument.layers]);// detecting current layers.
};
const installLayerEvent = () => {
// addNotificationListener receives parameter, firstly event names as an array and call back function,
// set, make , delete, all of them are events relative with layer.
// "listener" callback function will be fired when layer is added or deleted or renamed.
photoshop.action.addNotificationListener(["make", "set", "delete"], listener);
};
installLayerEvent();
any layer added, “make” event can detect it.
“set” event detects changed layer name and “delete” event detects deleting layer.
but to detect any kind of event on layer, you need to inspect more event I suppose.
there are more events on Photoshop.
Definitely, this is correct answer, because I missed what would trigger array update, and that is exactly this listener. Now its left to manage, to put it all together, pray for me