How to work with imported component

Hello,
I need help with my code. I am importing components from the team library. When I call the import function, I get back a JS promise. I don’t know how to work with this promise. I need to change the text and variants of components in this imported component.

My code:

async function importNode() {

let importComponent = await Figma.importComponentByKeyAsync(“5cad1f5ace96bf648”);

const favC = importComponent.findOne(node => node.type == “COMPONENT” && node.name == “Favorite”) as ComponentNode;

return favC;
}

How can I access the imported component and browse it and use the embedded components?

Thanks

This line is most likely unnecessary since the importComponent variable already stores a ComponentNode.

Find the TextNode you need in your imported component and change its value.

Or change the value of the component’s text property and the variant like this:

Sorry for my dumb questions, but i don’t know what you mean “variable already stores a ComponentNode”. Maybe I asked the wrong question.

Now I have an instance, but when I try set the property I get a error

let importComponent = await Figma.importComponentByKeyAsync(“5cad03bxxxxx”);

importComponent.createInstance()

importComponent.setProperties({‘Layout’: ‘Mobile’});

Error: Property ‘setProperties’ does not exist on type ‘ComponentNode’.

I don’t need to change only properties. My component includes a more component and Ii need access to all children’s components. And don’t know how :frowning:

How i get access to the component (name = favorite) which is inside the “ImportComponent” ??

Here are the lines from your original code snippet:

In the first line, you imported the component and stored it in a variable. In the second line, you are trying to find some other component in it, but this is impossible, because a component cannot contain other components, but only component instances (InstanceNode) and other objects.

The error speaks for itself. ComponentNode does not have such a property. You must use this method for the InstanceNode.

Please see the developer documentation:

Find the desired node in the children’s tree.

Thanks for your advice. I use the function children, and get back the children instance component. But when i trying to change property with .setProperties. But I’m getting an error does not exist on type ‘SceneNode’.

We are a bit confused, I don’t know how to change the text or property for embedded component instances {label} or instance favorite. Attached is an example of the structure of a component

Thanks

On which node was the setProperties method called?

Call the setProperties method on the “Participant” instance or change the characters property value of the “{Label}” text node.

// Set a value for a component's text property
pacticipantInstance.setProperties({'textPropertyName#id': 'Something'});

// Or change the value of the characters property
label.characters = 'Something';

Call the above method on the “Favourite” instance.

On which node was the setProperties method called?

this is my code:

let importComponent = await figma.importComponentByKeyAsync("5cad03b3xxx");
importComponent.createInstance()
importComponent.children[0].setProperties({'State': 'Like'})

I don’t know why you use this way, but I would do it like this:

let importComponent = await figma.importComponentByKeyAsync(key);
let instance = importComponent.createInstance();
instance.children[0].setProperties(obj);

Also make sure that children[0] is an instance.

And where do you see the error: in the Figma app console or in the IDE?

My mistake, I see the error in my IDE. But when I run the plugin in Figma it works.

Thanks.

I have a last thing. When I am using setProperities and choosing from options everything is good. But I don’t no how a set value for properties (set text).

My code:

instance.children[1].children[0].setProperties({‘Time’: ‘11:00’});

I want a set text for properties Time, but my code doesn’t work.

Thx.

Please re-read this topic and refer to the documentation.

Provide the full property name along with # and a unique ID.