skip to Main Content

I have an Astro component where I want to write text with tags. The problem is that the tags like "characteristic1" are not converted into HTML code, they stay as a string.

This component has a field for the characteristics of a product and it may have different size depending on the product:

<Plan
  title="title here" 
  subtitle="subtitle here" 
  characteristics={["characteristic1", 
                    "characteristic2",
                    ...
                   ]}
/>

This is represented by the following code in other file:

---
const { title, subtitle, characteristics } = Astro.props;
---

<p class="title">{title}</p>
<p class="subtitle">{subtitle}</p>

{characteristics?.map((item) => (
  <p class="text">{item}</p>
))}

It’s supposed that when you write HTML code in a string in JS it’s treated as HTML code but I don’t know why it’s treated as a string.

How can I convert the tags into HTML code? Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    As Keyboard Corporation says (I think that I can't mark his comment as the answer) it is solved by using <p class="text" set:html={item}></p>. Thanks for your answer!


  2. It seems that you are trying to set the text of something into a HTML element (as what I see lol)
    Check this out, it uses the .innerHTML property that edits the HTML property of an element.

    Mozilla InnerHTML

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search