How to Change the Appearance of Elements in an Array Using React.js

My situation involves an array receiving data from an API, and I'm looking to customize each button's appearance by giving them different background colors. In addition, my project includes Material UI... Below is the snippet of code I have -

{options.map((data, id) => (
 <Button className='p-3 md:w-[25wh] md:h-[25vh]' onClick={handleClickAnswer} fullWidth variant="contained">{decode(data)}</Button>
))}

I need guidance on how to style these buttons uniquely

Answer №1

There are various methods to apply distinct styles to different buttons. One common approach is utilizing string literals.

For instance,

  1. If your project utilizes bootstrap or tailwind for styling, you can do something like this:

{options.map((data, id) => (
     <Button 
       className=`p-${id} md:w-[25wh] md:h-[25vh]` 
       onClick={handleClickAnswer} 
       fullWidth 
       variant="contained">
        {decode(data)}
     </Button>
    ))}

Note that p-${id} will be adjusted for each item in the array

  1. Even without using bootstrap, you can adopt the same technique by defining custom classNames for individual button styles.
  2. If you want to apply a specific style to the first/last element, you can utilize the first child selector or last child selector.

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

Tips for effectively transferring data from useSelector to useState to avoid unnecessary re-renders

Is there a different way to pass data from redux to useState without causing multiple re-renders? Here is my current approach: function TestComponent(){ const dispatch = useDispatch(); const currentData= useSelector(state => state.currentData); con ...

Issue: Multiple requests being sent to the server using the axios.put method resulting in a Network Error. However, error is caught after

As a newcomer to react js, I am facing an issue when using the axios.put method on the React side. Here is the method in my React component: commitAssign() { let memberIds = this.state.assignedList; // update the data const headers = { "Content- ...

Deactivate participant's mic using AWS Chime SDK for JavaScript

Is there a way to mute specific attendees using Amazon Chime SDK JS v3.14 and also mute all attendees while utilizing the Amazon Chime SDK Component Library React? I attempted to mute attendees by sending a message via sendData, but unfortunately it did n ...

Using the useState hook to manage an array of boolean values in a React

As a beginner developer in React Native, I am facing a design pattern issue. I have multiple TouchableOpacity components in the same component and I need to maintain this structure. Each TouchableOpacity has an onPress function that changes its backgroun ...

NodeJS rendering method for HTML pages

We are in the process of developing a fully functional social networking website that will resemble popular platforms like Facebook or Instagram. Our plan is to utilize Node.js on the server side and we are currently exploring the best technology for rende ...

How to obtain the height of the parent screen using an iframe

Imagine a scenario where a div contains an image that is set to perfectly fit the height of the screen. This setup works flawlessly, with one exception - when placed within an iframe, the height of the div adjusts to match the content's height rather ...

Ensure that the text box inside the div adjusts its size in accordance with the dimensions of the window, as the div is already designed

As a novice in HTML and jQuery, I am currently working on creating a portfolio site for a school project. One of the challenges I have encountered is designing a graphic that includes a text box within a resizable div element. My goal is to prevent excessi ...

Issue with React when attempting to add a secondary class to Toggle component

Is there a way to add a second class to my <div> with the class "button"? When I try to append 'toggle-button', the class doesn't seem to work. <CSSTransitionGroup transitionName="buttonAninmated" transitionEnterTimeout={30 ...

Tips for positioning elements in the center and preventing them from getting cut off the edge of the screen using a horizontal scrollable div and the `justifyContent: "center"` property

I'm struggling to find a solution to center my horizontal scrollable div without relying on justifyContent: "center". The issue arises when I use justifyContent: "center", as it cuts off some of the elements inside the div when the scroll indicator ba ...

What is the best method to determine offsetTop in relation to the parent element instead of the top of

I have a straightforward inquiry: How can one determine the offsetTop of a child element in relation to its parent element rather than the top of the window? The definition of offsetTop indicates that it should give the distance by which a child element i ...

Issue with aligning items vertically using CSS and Javascript

I must admit, I am not well-versed in CSS. My goal is to create a performance bar using CSS and Javascript. So far, I have managed to generate a background bar with another bar inside that fills up to a specific percentage. However, my issue lies in the fa ...

Preventing a sprite from going beyond the boundaries of a canvas with pure JavaScript - here's how!

I am in the process of developing a game using native JavaScript, HTML, and a touch of CSS. I have two blocks called Sprites that tend to keep moving off the canvas edges when they reach them. Question: How can I utilize native JS to prevent the sprites fr ...

How can I ensure the button remains disabled until all inputs are completed in Redux form?

I am currently studying a react-redux form example that you can find at the following link: In this example, the submit button is enabled if the user fills in at least one input field. I am curious to know if there is a way to enable the button only when ...

Different requirements for the same peerDependency across various versions needed

Running npm i on my current react project triggers a warning about react peerDependency: npm WARN <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1664737775623b6277663b737762377662765057267632">[email protected]</a&g ...

Overlapping input elements occur when Twitter bootstrap prepended input elements are positioned side by side

I ran into an issue while attempting to place two input elements side by side, with a prefixed item on the first input, using display:table styles. Unfortunately, the second input element ends up overlapping the first. I tried using box-sizing: border-box ...

Is there a way to achieve this design using bootstrap?

I am aiming to create a layout similar to this. My goal is to have a fixed sidebar on the left that measures 330px wide. Additionally, I want a gray box positioned at the center of the screen that adjusts its position based on changes in the content above ...

Locate specific text within the content and apply an underline to it

Is there a way to locate specific text on my website and apply an underline to it? Suppose I have some text displayed and I wish to identify all instances of the word hello and underline them. Here is my attempt at the code: $( "body:contains('hell ...

When you click on the collapsible header background, the content collapses, but the main icon does not update

I am currently using collapsible content from Bootstrap and I am encountering an issue with the icon not changing when I click on the collapsible-header background. Here is a visual representation of the problem: 1st part (not collapsed) 2nd part (After ...

When the div is clicked, it changes color and then reverts back to its original color when another

I have encountered an issue where I need to differentiate the div I clicked on in order to avoid confusion, as the pop-up menu on my website will be identical with different links but the same buttons. I have been struggling to find a solution for quite so ...

Tips for highlighting a Material UI Button when pressing the Enter key

Once the user has completed the final input, they need to press the save button The 'Tab' key typically moves the focus to the next element (in this case a button), but I want to use the 'Enter' key for this action. Can anyone advise o ...