0

I have some strings stored in Firestore as:

item1: {
    content: '<p>Hi</p>'
}
item2: {
    content: '<p>Hi 2</p>'
}

When I use the following render function in my React component, it is printing out the paragraph tags (and any other HTML):

render() {
  const { items } = this.state

  this.items = items.map(function(item, key) {
    <li key={key}>{item['content']}</li>
  })

  return (
    <ul>
      {this.items}
    </ul>
  )
}

How to fix? Any way to do this without dangerouslySetInnerHtml?

4
  • why dont you want to use dangerouslySetInnerHtml? Commented Nov 25, 2019 at 19:27
  • It seems dangerous! The item content is user-generated, so does that make the app vulnerable? Commented Nov 25, 2019 at 19:27
  • it depends on where your content comes from, I think it is the most common way to insert html to your react code. Commented Nov 25, 2019 at 19:31
  • Users are inputting the HTML content in the app, then this component is printing it out somewhere else. I've heard that dangerouslySetInnerHtml should be avoided if possible, so seeing if this would be a case where it would make sense to use it. Commented Nov 25, 2019 at 19:52

1 Answer 1

1

Ended up using react-html-parser package for this:

https://www.npmjs.com/package/react-html-parser

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.