0

I'm creating a simple React application, and I have to map through an array like this:

const data = [
   {
    id: 1,
    string: "my dog is a <strong>poodle</strong>"
},
{id: 2,
 string: "my cat is nice"

}

]

Sometimes inside the string, I have some strong tags to render. When I try to render that sentence, I will see the html rendered. If I try to use a template literal and assign that strong tag to a variable, I will have the [object object] error. How can I render tags in this situations? Trying if possible to avoid the "setDangerouslyHTML" prop. This is my mapping function:

data.map(da=>{

  return (<li key={da.id}>{da.string}/>)
}

Thanks for the help!

1 Answer 1

1

You didn't properly close the tag

data.map(da=>(<li key={da.id}>{da.string}</li>))

Edit If you want to display the tag in the string , you can do

data.map(da=>(<li key={da.id} dangerouslySetInnerHTML={{ __html: da.string }}></li>))

Here is a working example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.