[SalesForce] How to migrate aura:id to Lightning Web Components

How do I migrate a

<div aura:id="foo"></div>

from Aura to LWC when I use Aura Id for finding?

cmp.find("foo");

Best Answer

I have found the LWC docs to offload some of this to the MDN docs, so bringing this one layer higher (in the actual LWC docs would be nice) would have made it easier for us.

In scenarios where there are two components of the same type on your template, the equivalent to aura:id is to use a data-id tag and select it like this:

this.template.querySelector('[data-id="userform"]')

Related Topic