Skip to main content
Floran
March 2, 2023
Solved

Find all instance coming from the same component

  • March 2, 2023
  • 15 replies
  • 2378 views

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…

This topic has been closed for replies.
Best answer by tank666
node.mainComponent && node.mainComponent.remote && node.mainComponent.parent && node.mainComponent.parent.name === 'something'

15 replies

Floran
FloranAuthor
March 5, 2023

This doesn’t look working for me :

const instances = figma.root.findAll( node => node.mainComponent.remote && node.mainComponent.parent && node.mainComponent.parent.name === target);
console.log(instances);

Error: in findAll: “findAll” callback crashed: TypeError: cannot read property ‘remote’ of undefined

Floran
FloranAuthor
March 5, 2023

Make sense, maybe not optimal.

tank666
tank666Answer
March 6, 2023
node.mainComponent && node.mainComponent.remote && node.mainComponent.parent && node.mainComponent.parent.name === 'something'
UX/UI designer. Figma expert. Creator and developer of plugins and widgets for Figma and FigJam. Check out my resources in Figma Community: figma.com/@tank
Floran
FloranAuthor
March 7, 2023

Thanks a lot, this works. So if I understand, I need to check that all the values exists in any case.

March 21, 2023

I just want to point out that optional chaining makes this a lot nicer:

node.mainComponent?.remote && node.mainComponent.parent?.name === 'something'