🎁

12 Days of Baddass Courses sale! Get instant access to the entire egghead library of courses and lessons for 58% off.

Runs out in:
15 : 05 : 54 : 12
Join egghead, unlock knowledge.

Want more egghead? It's 58% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 40% Off
1×
Become a member
to unlock all features
Autoplay

    Render HTML directly into a component in Svelte 3

    Tomasz ŁakomyTomasz Łakomy
    sveltesvelte

    Disclaimer: please use this feature carefully.

    Sometimes we need to render HTML (that comes from a source that we trust!) directly into a component, for instance when we're using a WYSIWYG editor that we control.

    In this quick lesson we're going to learn how to use @html in order to render HTML markup from a textarea into a div element.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: We have this simple app with a text area component. Whatever we are going to type in, inside of this text area, we would like to render inside of this diff over here. To do that, we need to bind the value of this text area to this stringToRender variable.

    In order to do that, bind value = stringToRender. Right now, if I type "Hello Egghead," this text is going to appear over here. I would like to be able to paste in some HTML, and have it rendered inside of this diff. In order to do that, we need to add the @html syntax in front of this string to render variable.

    If I save, and paste this HTML again, we're going to see it rendered over here. Bear in mind that if you decide to use this feature, Svelte is not going to sanitize any string that you decide to render using @html, so you have to be careful not to expose your users to an attack.