How to replace local component to component from library

Hello folks,
I need to replace all component instances on my page to components from library. I try using the function swapComponent but I don’t know if it is the right path. Here is my code:

const old_com = figma.root.findOne(node => node.type == "INSTANCE" && node.name == "Icon") as InstanceNode; 
let new_com = await figma.importComponentByKeyAsync("7c5dd1fbdbd83b87ac6d7XXX");

old_com.swapComponent(new_com)

Nothing happens after starting my code.

Thanks

  1. You use the find…() method throughout the entire document.
  2. This method (findOne) returns only the first instance found.

So, to swap all the components, you need to use the appropriate methods. For example, if you need to find instances on a specific page, call the find method on that page.

PageNode.findAll(/* your conditional logic */)

Then iteratively call the swapComponent method on each instance.