Mapping with Star Components

As a newcomer to ReactJs, my task is to create a 5-star review component with the ability to display half-star ratings. I previously implemented this in Angular5.

Within the map method, I need to loop through the following elements:

 <!-- full star -->
 <input 
      id="'star'+num"
      value="num-0.5"
      name="rating" 
      type="radio" />
 <label className="full"></label>
 <!-- half star -->
 <input
   value="num-0.5"
   id="'halfstar'+num" 
   name="rating" 
   type="radio"  />
 <label className="half"> </label>

An error occurs because it requires a wrapper div element. However, if we use a div, the CSS for the star review won't function properly. The initial example was taken from the link provided here:

https://codepen.io/jamesbarnett/pen/vlpkh

In Angular, this issue is resolved by using ng-container. You can see an example below:

Is there a solution to this problem in React?

Answer №1

In React, the class keyword is reserved for declaring components, so you should use className in JSX tags instead. Wrap your code within a div element and utilize className rather than class for it to function correctly.

See the example below for a working demonstration:

<div>
      <input 
         id="'star'+num"
         value="num-0.5"
         name="rating" 
         type="radio" />
     <label className="full"></label>

     <input
          value="num-0.5"
        id="'halfstar'+num" 
        name="rating" 
        type="radio"  />
    <label className="half"> </label>
</div>

When upgrading to React V16, you can even return without the need for a div by using fragments. Fragments allow you to return strings as well. For more details, refer to the documentation here.

Remember to set a key for each element to enhance performance and prevent warnings.

The structure of a fragment return is essentially an array like this:

return [
     <input />
     <input />
 ]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Google Visualization chart fails to display properly once CSS is applied

Struggling with a graph display issue here. It's quite perplexing as it works fine on older laptops and Safari, but not on Chrome or older versions of Firefox. Works like a charm on my old laptop and Safari, but fails on Chrome and Firefox (haven&apo ...

Clickability of links on an element is lost when it is positioned behind another element that is fixed

I can't seem to figure out why the green menu is hidden behind the white header, making its links unclickable. It's necessary for the layout of the page, but it's causing this issue. The white header has a position: fixed; attribute. The gr ...

Angular material chips showcase a row of five chips

I am working with the basic mat-chips from angular material and I want to arrange them in rows of five without increasing their size: Chip1 Chip2 Chip3 Chip4 Chip5 ..................(space left) Chip6 Chip7 Chip8 Chip9 Chip10 ...............(space le ...

Eliminating the boundaries of Bootstrap accordion

I'm currently working on creating Bootstrap accordion menus and I'm attempting to remove the border that appears when it's collapsed and expanded. Below is the code I've been working on: <div class="accordion"> <div ...

Error message: When using the Tab Element with Next.js and Semantic UI, an error occurs stating that the element

Currently, I am attempting to integrate semantic-ui tabs into my Next.js application. An error message that I encounter reads: The element type is invalid: was expecting a string (for built-in components) or a class/function (for composite components ...

Reacting like sticky bottoms and tops

I'm working on a react/tailwind project that involves a component I want to be fixed at both the top and bottom of the screen. In simpler terms, there's an element that I need to always stay visible even when the user scrolls up or down the page ...

Ways to troubleshoot React JSX/HTML input issue displaying as -$NaN.undefined

https://i.sstatic.net/1xIvb.pnghttps://i.sstatic.net/aeQrS.pngI'm currently facing an issue while trying to implement an input field that subtracts a number from another computed value within an HTML table setup. I've noticed that everything work ...

Images with spaces in their names are failing to load in React Native

I am currently trying to utilize an image within my react native application. At this point, my code is very minimal. The following code snippet is all I have for now: import React from 'react'; import { ScrollView, View, Text } from 'reac ...

Achieve automatic updates on React + Django without the need to execute "npm run build" every time

I've managed to integrate my Django + React app successfully for the most part. However, I find it tedious that every time I need to make changes to my React code, I have to stop Django from running and then run npm run build again. Despite reading d ...

Struggling with changing the state in React following an API request

I am currently working on developing a textbox that features autocomplete by fetching suggestions from an API. However, I am facing challenges with updating the state after retrieving the array from the API endpoint. The code I am modifying can be found he ...

Using the && operator for conditional rendering in a React return statement

How should I format my return statement in a class component when using the && operator in the condition like this: if (x === null && y === null) If the condition is met, display this HTML: <div className="col-12"> <SomeComponent /> < ...

Positioning JQuery tooltips

I've been developing a simple tooltip tool (check out the fiddle link below), but I'm encountering some issues with positioning. My goal is to have the tooltip appear centered and above the clicked link, however right now it appears at the top le ...

Is there a way to take a snapshot of an HTML Canvas frame and showcase it on a Bootstrap modal?

I have a page where users must grant permission for their webcam or camera. Once granted, a webmoji will move according to the user's face position. Below is a button that will turn blue and become active once a face is detected. When clicked, I want ...

What are the steps to implement character movement in a 2D game using JavaScript?

I'm having trouble getting the image with the ID "yoshi" to move around in my 2D game document.onkeydown = (e) => { if (e.keyCode == 37) yoshi.style.left = yoshi.offsetLeft - 5 + "px"; else if (e.keyCode == 38) yoshi.style.top = yoshi.offset ...

CSS can always surprise with its unexpected animations

Currently developing a static website with CSS animations: * { -webkit-transition: all 1s ease-in-out; -moz-transition: all 1s ease-in-out; -o-transition: all 1s ease-in-out; -ms-transition: all 1s ease-in-out; transition: all 1s ease- ...

Troubleshooting image overlay alignment concerns

My script successfully adds the image src to the overlay's image, but I encounter a problem when creating the variable imgH for the margin-top value. It calculates the image's height and divides it in half. The issue arises when the calculated h ...

I am looking to conceal the y-axis labels and tooltip within the react chart

I am currently working with react-chart-2. I have a line graph that displays a tooltip when hovered over, but I would like to hide this tooltip feature. Additionally, I want to remove the numbers 0, 0.1, 0.2 up to 1 on the left side (y-axis) of the gra ...

When hovering over the child element, the hover effect on the parent element should be disabled

I'm dealing with a situation where I have two nested <div> elements. <div class="parent"> <div class="child"></div> </div> The challenge here is that I want to change the background of the .parent ...

The Heroku application running with Express React Server-Side Rendering has experienced a crash

I'm currently attempting to deploy my basic node application on the Heroku platform. Below is the server.js file, which serves as the entry point: require('babel-register') const express = require('express') const React = requi ...

Color highlighting in dropdown HTML items

I'm trying to create a dropdown menu with alternating colors for the items. Can someone please provide the CSS code needed to style dropdown list items using ODD and EVEN? ...