Tuesday 8 August 2023

Is there a convenient way to reference a DOM element in Svelte components?

I am used to libs/frameworks like React or Angular which both have convenient ways to access actual DOM elements that belong to logical components. React has the createRef utility and Angular has among other things the template variables in combination with eg. @ViewChild.

Those references not only make it easy to access the DOM without querying the elements explicitly every time , they also stay up to date with the DOM so that they always hold reference to the current element. I just started with Svelte for my pet project but although I went through Svelte's documentation and google a lot, I didn't find anything similar in concept & usage. I suppose it might have something to do with the Svelte's runtime-less concept, but still don't know why there wouldn't be such a utility.

So the question is, is there a similar utility in Svelte?



from Is there a convenient way to reference a DOM element in Svelte components?

No comments:

Post a Comment