I'm using commerce js on my website, but in the description box, it displays a <p> tag

506 Views Asked by At

I'm creating my first website with reactjs and commerce js, but the description box in commerce js isn't functioning.

This is description box of commerce js:

this is description box of commerce js

This is my code in react:

this is my code in react

Here is p tag in output:

Here is p tag in output

2

There are 2 best solutions below

0
scrowler On

Commerce.js uses HTML in the description field for products. Modern JavaScript frameworks such as React will not allow you to render external HTML by default for security reasons. You have two options:

  1. You can use React's dangerouslySetInnerHTML method, if you trust the Commerce.js API's response:
<p dangerouslySetInnerHTML={{__html: product.description}} />
  1. You can strip HTML from the product.description variable and render plain text instead. Example package to help with this might be https://www.npmjs.com/package/striptags
0
Armin Čičić On
  1. I simply used .slice(3,-4) method on description string and it does the job.

  2. Also, you can type dangerouslySetInnerHTML={{__html: product.desctiption}}