Hey !
I would like to do something on every instance of a same component, coming from an external library.
Let’s say the component is named “XYZ” and has multiple variants.
I know that I need to use *instance*.mainComponent.parent.name to get the name of the component, because if not it gives the name of the variant (xxx = xxx, yyy = yyy …)
But when I use it as a search criteria, it’s not working : const instances = figma.root.findAll( elem => elem.mainComponent.parent.name === "Spec guides");
I get the error :
Error : “findAll” callback crashed: TypeError: cannot read property ‘parent’ of undefined
How to do it properly? Thanks !
PS : I tried to use the name of the instance. It’s working, but sometimes we rename the instance so it won’t work in this case…
The findAll method will return all nodes for you according to the condition. In your example, this would be both ComponentNode and ComponentSetNode (local and remote nodes). If the node is remote and this node is a ComponentNode, then its parent property will be null. Hence, you are seeing the above error. To fix this, you just need to write the condition correctly.
That’s correct, parent is null for remote components.
Also, if you have a component node already, it’s much easier to do componentNode.instances to get all the instances instead of traversing the document: