Skip to main content
Floran
Mar 2, 2023 10:04 PM UTC
Solved

Find all instance coming from the same component

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.
tank666
tank666Solution
Mar 6, 2023 7:12 PM UTC
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

15 replies

Floran
FloranAuthor
Mar 5, 2023 12:35 PM UTC

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
Mar 5, 2023 12:36 PM UTC

Make sense, maybe not optimal.

tank666
tank666Solution
Mar 6, 2023 7:12 PM UTC
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
Mar 7, 2023 8:52 PM UTC

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

Mar 21, 2023 2:12 AM UTC

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

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