Skip to main content
Question

Is there any way to check if a instance is up-to-date


Aaron_Xu

I want to writing a script that can update all instance inside a frame which is selected. So I need to check and update instance inside. But I can’t find a right api to do that.

This topic has been closed for comments

6 replies

Gleb
  • Power Member
  • 4706 replies
  • April 3, 2024

What do you mean by update instances? Which API specifically can you not find?


Aaron_Xu
  • Author
  • New Participant
  • 19 replies
  • April 5, 2024

When I use Figma for design, if a instance has a newer component version in team library, it show symbol like this:
image
And I was wondering if there a API that can check if a instance “can update”.


Aaron_Xu
  • Author
  • New Participant
  • 19 replies
  • April 5, 2024

You know, for my Figma plugin development.


Gleb
  • Power Member
  • 4706 replies
  • April 5, 2024

Unfortunately there is no direct API for that but it’s possible to do.

To find if instance is up to date:

  • Get its main component ID and import it
  • Check if importedComponent === instance.mainComponent
  • If they are different, the instance can be updated

To update the instance:

  • instance.swapComonent(importedComponent)

Aaron_Xu
  • Author
  • New Participant
  • 19 replies
  • April 5, 2024

OK, let me try it later


Aaron_Xu
  • Author
  • New Participant
  • 19 replies
  • April 12, 2024

It’s work!
I post my function code in case someone needs it

async function checkIfInstanceIsUpdated(instance: InstanceNode) {
    try {
      const asyncMainComponent = await instance.getMainComponentAsync()
      if (asyncMainComponent != null) {
        const importedComponent = await figma.importComponentByKeyAsync(asyncMainComponent.key)
        if (importedComponent != null) {
          return (importedComponent.id == asyncMainComponent.id)
        }
      }
      return null
    } catch (error) {
      console.log("error ! " + error)
      return null
    }
  }
  async function checkAndUpdateInstance(instance: InstanceNode) {
    try {
      const asyncMainComponent = await instance.getMainComponentAsync()
      if (asyncMainComponent != null) {
        const importedComponent = await figma.importComponentByKeyAsync(asyncMainComponent.key)
        if (importedComponent != null) {
          if (importedComponent.id != asyncMainComponent.id) {
            console.log("update")
            instance.swapComponent(importedComponent)
            return instance
          }
        }
      }
      return null
    } catch (error) {
      console.log("error ! " + error)
      return null
    }
  }

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings