3

This is code in a JavaScript object

const AlbumDetails = [
{
    name: '<i class="fas fa-check">Basic</i>',
    price: '',
    details: [],
},
{ name: '<i class="fas fa-check">Intermediate</i>', price: '', details: [] },
{ name: '<i class="fas fa-check">Pro</i>', price: '', details: [] },
{ name: '<i class="fas fa-check">Ultimate</i>', price: '', details: [] },
];

export default AlbumDetails;

and I want to use this object like

const PriceCard = (props) => {
<div className="p-box">
                    
    <div className="p-content">
        <h2>{props.AlbumDetails[0].name}</h2>
        <h4>{props.AlbumDetails[0].price}</h4>
        
        
    </div>
</div>
}

so on. In the browser, it shows <i class="fas fa-check">Basic</i>, but I want only the icon and name "Basic" only. So how it could be done.

3 Answers 3

1

The HTML string can be inserted using dangerouslySetInnerHTML:

<h2 dangerouslySetInnerHTML={{ __html: props.AlbumDetails[0].name }} />

Note: you need to be satisfied that the HTML is safe (i.e is not formed from user input), otherwise you could open yourself up to XSS.

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

2 Comments

Thank you a lot, It's work. you save my day
Does this try to manually insert the tag in DOM?
1

Instead of keeping the entire JSX in the Array, keep only the words in array

const AlbumDetails = [
{
    name: 'Basic',
    price: '',
    details: [],
},
{ name: 'Intermediate', price: '', details: [] },
{ name: 'Pro', price: '', details: [] },
{ name: 'Ultimate', price: '', details: [] },

And keep the JSX in the component and instead only access the content like this

const PriceCard = (props) => {
<div className="p-box">
                    
    <div className="p-content">
        <h2><i class="fas fa-check">{props.AlbumDetails[0].name}</i></h2>
        <h4>{props.AlbumDetails[0].price}</h4>
    </div>
</div>

1 Comment

Yeah, that's what I thought, but I have to reuse that priceCard component, Thank You.!!
1

From your example, I see you are trying to show multiple elements of the same data source. You can use JavaScript map() function so that you don't have refer them using indices and avoid repeated lines of code. Please refer the below code.

const AlbumDetails = [
  {
    name: '<i class="fas fa-check">Basic</i>',
    price: "",
    details: []
  },
  { name: '<i class="fas fa-check">Intermediate</i>', price: "", details: [] },
  { name: '<i class="fas fa-check">Pro</i>', price: "", details: [] },
  { name: '<i class="fas fa-check">Ultimate</i>', price: "", details: [] }
];

const PriceCard = (props) => (
  <div className="p-box">
    <div className="p-content">
      {props.AlbumDetails.map((album) => (
        <div key={album.name}>
          <h2>{album.name}</h2>
          <h4>{album.price}</h4>
        </div>
      ))}
    </div>
  </div>
);

You can read more about this here

1 Comment

Yeah, I will check that also.. Thank you.

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.