Ausnahme gefangen: SSL certificate problem: certificate is not yet valid ๐Ÿ“Œ How to Make Star Rating in React

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š How to Make Star Rating in React


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

For my capstone project at Flatiron School, I created an application in which users can track and manage the eateries (ex: restaurants, cafes/bakeries/coffee shops, & bars/breweries) they have visited and the food and drink they ordered so visits can be referenced at a later time.

One of the features I wanted to include was a rating functionality. Instead of displaying just numerical ratings, I wanted to display those ratings via star icons. In addition to displaying those numerical ratings into stars, I wanted users to be able to click on the stars to create or edit their rating for an eatery.

I used a React frontend to accomplish this task. I will first describe how to display ratings via stars, and then describe how to dynamically change the star ratings when creating or editing a rating.

How to Convert Numerical Rating into Star Rating

Inside the component that holds and displays the eatery rating, I first declared a constant that holds the colors of the stars when they are empty and filled (this can alternatively be done via inline styling).

const colors = {
        orange: "#F2C265",
        grey: "a9a9a9"
    }

One of my attributes of an Eatery is the "rating" declared as an integer on the backend. This numerical, integer rating was then translated into stars in React. To create the stars, I first installed the React Icons Kit library and selected the FaStar icon, and imported it into my component. I then created an array of 5 stars, called "stars", where the .fill was 0.

const stars = Array(5).fill(0)

Now, in my return, this is where the star rating is displayed on the page. I mapped over the star array and returned the star icon where the color of the star is filled based on the rating of the specific eatery. For example, if a user gave the eatery 4 stars, 4/5 stars would be filled in with the color = orange, and 1/5 stars would remain the color = grey. (I also chose to display the numerical star rating next to the star icons.)

return (
  <div>
     {stars.map((_, index) => {
        return (
             <FaStar
                 key={index}
                 size={24}
                 color={(rating) > index ? colors.orange : colors.grey}
              />
      <p>({rating} Stars)</p>
         )
      })}
   </div>
);

Example

How to Dynamically Create and Edit Star Icon Rating

Display a static rating via star icons is more simplistic than creating dynamically changing star icons. Dynamically creating or editing star icon ratings requires useState and event listeners. The same React icon was imported and the same color constant and star array described above were also created.

First, inside the component where I want to create (or edit) a star icon rating, I created two states. One for the rating attribute, with the initial state set as the current rating (if the user was editing the rating). The other state was created for selecting the stars when rating called "hoverValue". Initial state for the rating attribute was set as the current rating (if the user was editing the rating). Initial state was declared as undefined because we don't want to give the hoverValue a pre-determined state - we want it to be 0 before we start rating.

const [rating, setRating] = useState(eatery.rating)
const [hoverValue, setHoverValue] = useState(undefined)

When rating, I wanted to hover over the stars and have them be filled with the orange color before settling on the final rating. To do this, I added a some event listeners to handle this functionality:

const handleMouseOverStar = value => {
    setHoverValue(value)
};

const handleMouseLeaveStar = () => {
    setHoverValue(undefined)
}

The handleMouseOver takes in a value when the user hovers over the star icons and sets that value in the setHoverValue setter function. The handleMouseLeaveStar removes the stars when an user moves away from the stars (i.e. wants to reduce the amount of stars they have chosen).

const handleClickStar = value => {
    setRating(value)
};

The handleClickStar takes the value of the rating a user choses via a mouse click and sets that value to the setRating setter function. This means, the number of stars a user selects will be the rating of the eatery.

Now, we need to actually display this functionality on the page. In the return, we are going to map over the stars array (like before) and now include the event listeners.

return (
  <div>
     {stars.map((_, index) => {
          return (
               <FaStar
                   key={index}
                   size={24}
                   value={rating}
                   onChange={(e) => setRating(e.target.value)}
                   color={(hoverValue || rating) > index ? colors.orange : colors.grey}
                   onClick={() => handleClickStar(index + 1)}
                   onMouseOver={() => handleMouseOverStar(index + 1)}
                   onMouseLeave={() => handleMouseLeaveStar}
                />
           )
       })}
  </div>
);

The onChange event listener manages the setting of the rating, the onClick event listener manages the incrementation of the star icon rating, and the onMouseOver and onMouseLeave manage the hover effects of the star rating.

It is difficult to show the functionality of this hover star rating via images, but if you are curious how this works, you can play around with it on my website:

I think this functionality is a fun feature to add to any website that includes rating, and I hope this tutorial was helpful!

...



๐Ÿ“Œ How to Make Star Rating in React


๐Ÿ“ˆ 36.79 Punkte

๐Ÿ“Œ PHPJabbers Star Rating Script 4.0 Rating Item Stored cross site scripting


๐Ÿ“ˆ 35.54 Punkte

๐Ÿ“Œ PHPJabbers Star Rating Script 4.0 Rating Item Stored Cross Site Scripting


๐Ÿ“ˆ 35.54 Punkte

๐Ÿ“Œ Creating a Dynamic Star Rating System in React


๐Ÿ“ˆ 29.93 Punkte

๐Ÿ“Œ Easy Customizable Star Rating Component in React


๐Ÿ“ˆ 29.93 Punkte

๐Ÿ“Œ Genius Kids Give One-Star Rating to Homework App to Make Apple Remove It


๐Ÿ“ˆ 27.8 Punkte

๐Ÿ“Œ This Week In React #127: Nextra, React-Query, React Documentary, Storybook, Remix, Tamagui, Solito, TC39, Rome...


๐Ÿ“ˆ 26.97 Punkte

๐Ÿ“Œ This Week In React #131: useReducer, Controlled Inputs, Async React, DevTools, React-Query, Storybook, Remix, RN , Expo...


๐Ÿ“ˆ 26.97 Punkte

๐Ÿ“Œ This Week In React #139: React.dev, Remix, Server Components, Error Boundary, Wakuwork, React-Native, Bottom Sheet...


๐Ÿ“ˆ 26.97 Punkte

๐Ÿ“Œ This Week In React #142: React-Query, Million, OpenNext, Ariakit, Expo-Image, React-Three-Fiber, TS 5.1, Node.js 20, WebGPU...


๐Ÿ“ˆ 26.97 Punkte

๐Ÿ“Œ This Week In React #146: Concurrency, Server Components, Next.js, React-Query, Remix, Expo Router, Skia, React-Native...


๐Ÿ“ˆ 26.97 Punkte

๐Ÿ“Œ React: 5 Small (Yet Easily Fixable) Mistakes Junior Frontend Developers Make With React State


๐Ÿ“ˆ 24.84 Punkte

๐Ÿ“Œ How To Make Login Page Like Twitter Using React Js | Sign In Page Design With React Js


๐Ÿ“ˆ 24.84 Punkte

๐Ÿ“Œ WordPress Rating-Widget: Star Review System 2.8.9 Information Disclosure


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ WordPress Rating-Widget: Star Review System 2.8.9 Information Disclosure


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ #0daytoday #WordPress Rating-Widget: Star Review System 2.8.9 Information Disclosure Vulnerability [#0day #Exploit]


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Rating-Widget: Star Review System <= 2.8.9 - Enable Debugging


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Gemaltoโ€™s Channel Program Receives a 5-Star Rating in CRNโ€™s 2019 Partner Program Guide


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Star Wars Jedi Fallen Order: USK-Rating fรผr New-Gen-Konsolen aufgetaucht


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ This Bree AI Chatbot app has a 4.7-star rating, showing that sometimes all people want is somebody to talk to


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Tesla Model X the First SUV Ever To Achieve 5-Star Crash Rating in Every Category


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Tesla Model 3 Earns Five-Star Crash Safety Rating From NHTSA


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Halo Wins 5-Star Rating from SC Magazine


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ ๐ŸŒŸ5 Star Rating component: A Step-by-Step Guide ๐ŸŒŸ


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ [webapps] kk Star Ratings < 5.4.6 - Rating Tampering via Race Condition


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Creating a Multicolored Star Rating Card Component


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ 2021 Toyota Innova Scores 5 Star Safety Rating In ASEAN NCAP Crash Test


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Robinhood Plummets Back Down To a One-Star Rating on Google Play


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Fortra Receives Prestigious 5-Star Rating in CRNยฎ 2022 Partner Program Guide


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Creating an Interactive Multicolored Star Rating Component


๐Ÿ“ˆ 20.94 Punkte

๐Ÿ“Œ Alle Star-Wars-Spiele ab 2023: Star Wars Eclipse, Star Wars Civilization und mehr


๐Ÿ“ˆ 19.01 Punkte

๐Ÿ“Œ Star Trek - Picard: Patrick Stewart wรผnscht sich ein &quot;Star Wars/Star Trek&quot;-Crossover


๐Ÿ“ˆ 19.01 Punkte

๐Ÿ“Œ Star Trek - Picard: Patrick Stewart wรผnscht sich ein &quot;Star Wars/Star Trek&quot;-Crossover


๐Ÿ“ˆ 19.01 Punkte

๐Ÿ“Œ Star Trek: Eine &quot;Star Trek II&quot;-Anspielung hรคtte beinahe ein Logikloch in &quot;Star Trek Beyond&quot; gelรถst!


๐Ÿ“ˆ 19.01 Punkte

๐Ÿ“Œ Star Trek: Wird dieser &quot;Discovery&quot;- und &quot;The Witcher&quot;-Star durch einen ikonischen DS9-Star ersetzt?


๐Ÿ“ˆ 19.01 Punkte











matomo