Sharryhong's Front-End
  • About Sharryhong's Front-End Blog.
  • Front-End Dev
    • Flutter
      • API Key 안전하게 숨기기. Cloudflare Workers!
      • Web CORS 에러
    • Vue.js
      • usePagination - composition api (vue2)
      • Pagination Component
      • Vue Image Skeleton
      • Sort an Array of Objects by Boolean property
      • Dynamic Components
      • LoadingBar Component
      • composition-api 사용하여 menu 의 outside click 시 close
      • vue+TypeScript
      • Vue.js 개발자를 위한 VS Code
      • Vue3 Chrome Extension
      • input file 선택된 파일 명 reset하기
      • element 내부 scroll 될 때, ScrollTop 값에 따라 div를 보이게, 안보이게 제어하기
      • 진입한 url path에 맞게 메서드 실행
    • React
      • 절대경로(absolute path) 설정, jsx파일로 이동 설정
      • useContext rerender issue
      • Firebase v9 Auth with React
      • Firebase v9 Realtime Database with React
      • React Star Rating (별점 표시)
      • React App deploying (배포)
    • Next.js
      • CSR, SSR, Next.js
    • API Mocking
      • Vue 에서 msw 적용하기
    • Storybook
      • styled-component theme.ts가 storybook엔 적용안되는 이슈
      • storybook에서 alias path 안되는 이슈
      • storybook에 Global Style 적용하기
  • etc
    • brew update
    • npm upgrade (mac)
  • DEV BETTER
    • Clean Code
      • Clean Code JavaScript 강의
        • Variables - 변수 다루기
        • Boundary - 경계 다루기
        • Conditional - 분기 다루기
        • Array - 배열 다루기
      • Tip. 클린 코드를 위한 5가지 팁
      • Tip. 코딩 잘하는 팁 3가지
    • 웹 성능 최적화 (performance)
      • performance 강의
        • loading, rendering performance
        • Tools
        • Image size optimization
        • bottleneck (병목) code 탐색
Powered by GitBook
On this page

Was this helpful?

  1. Front-End Dev
  2. React

React Star Rating (별점 표시)

2022-03-02

PreviousFirebase v9 Realtime Database with ReactNextReact App deploying (배포)

Last updated 3 years ago

Was this helpful?

star.svg 를 사용하여 별점표시 컴포넌트를 만들어보았습니다.

개발환경 : React, Typescript

결과화면 :

src/assets/star.svg // 별 하나

<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24">
  <path d="M12 .587l3.668 7.568 8.332 1.151-6.064 5.828 1.48 8.279-7.416-3.967-7.417 3.967 1.481-8.279-6.064-5.828 8.332-1.151z"/>
</svg> 

StarRating.tsx

import React from "react";
import { ReactComponent as Star } from "assets/star.svg";

interface RatingType {
  rating?: number;
  maxRating?: number;
}

const StarRating = ({ rating, maxRating = 5 }: RatingType) => {
  return (
    <>
      {[...Array(maxRating)].map((star, i) => {
        if (rating && i < rating) {
          return <Star key={`${star}${i}`} fill="#ffc500" />;
        }
        return <Star key={`${star}${i}`} fill="#ccc" />;
      })}
    </>
  );
};

export default StarRating;
  • props로 받은 rating 만큼 노란색 (#ffc500)으로 표시하고,

    나머지는 회색(#ccc)으로 표시합니다.

  • maxRating 는 별 갯수로, 상위컴포넌트에서 props로 주지 않으면 기본값 5개로 셋팅됩니다.

사용시 예

<StarRating rating={rating} />