简体   繁体   中英

How to avoid dangerouslySetInnerHTML?

I wrote a React component which renders rating and visualizes it with stars. But I had to use dangerouslySetInnerHTML for that. How could I rewrite component with better code style and without dangerouslySetInnerHTML? Thank you for any help.

export default class BookRating extends React.Component {
  renderStars = () => {
    let result = '';

    for(let i=1; i<=5; i++) {
      this.props.rating >= i
      ? result += '<div class="fa fa-star checked"></div>'
      : result += '<div class="fa fa-star"></div>'
    }

    return result;
  }

  render () {
    return (
      <div className="rating" dangerouslySetInnerHTML={{ __html: this.renderStars() }} />
    );
  }
}

You can render multiple elements with an array, rather than a string:

export default class BookRating extends React.Component {
  renderStars = () => {
    let result = [];

    for(let i=1; i<=5; i++) {
      this.props.rating >= i
      ? result.push(<div key={i} class="fa fa-star checked"></div>)
      : result.push(<div key={i} class="fa fa-star"></div>'
    }

    return result;
  }

  render () {
    return (
      <div className="rating">{this.renderStars()}</div>
    );
  }
}

As Quentin and Emile pointed out, you can write this function in one line, like so:

renderStars = () => Array(5).fill(0).map(_, i => <div className={`fa fa-star${this.props.rating > i ? ' checked' : ''}`}/>)

I did something similar that also supported half-stars and showed “empty” stars for the remaining spots (eg 3.5 stars would show 3 full stars, 1 half star, and 1 empty star):

const Rating = ({ rating }) => {
  rating = Math.round(rating * 2) / 2
  const fullStars = Math.floor(rating)
  const halfStars = rating - fullStars > 0.2 ? 1 : 0
  const emptyStars = 5 - fullStars - halfStars

  return (
    <div css={{ whiteSpace: "nowrap", ".fa": { color: "yellow" } }}>
      {[...Array(fullStars)].map((_, index) => (
        <span key={`f-${index}`} className="fa fa-star" />
      ))}
      {[...Array(halfStars)].map((_, index) => (
        <span key={`h-${index}`} className="fa fa-star-half-o" />
      ))}
      {[...Array(emptyStars)].map((_, index) => (
        <span key={`o-${index}`} className="fa fa-star-o" />
      ))}
    </div>
  )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM