Utilizing the dynamic flair with React's map functionality

In my display area, which is a div element, I have rendered some text using spans. I utilized the map function to render the text from an array.

The issue arises when I apply a color class to the span elements within the map function, causing all text to have the same color. I understand why this is happening, but I only want a specific word (a single word I target) to have the color while the rest remain unchanged. I am currently unable to find a solution to this problem.

The word I want to trigger the color change is "colorTrigger".

Here is a snippet of the code:


<div className="firstLine">
{display.map(word => {
    if (colorTrigger) {
        return (<span key={uuid()} className="singleWord redColorWord">{word}</span>)
    }
    return (<span key={uuid()} className="singleWord">{word}</span>)  
})}

Answer №1

To apply a condition to the className itself, you can check for the existence of colorTrigger. If it is present, you can use a similar logic to your if statement on the className conditionally.

<div className="firstLine">
  {
   display.map(word=>{
      return (<span key={uuid()} 
               className={`${word === 'colorTrigger' ? 'redColorWord' : null} 
                          singleWord`}>
              {word}
             </span>)
   })
  }
 </div>

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

Is this the proper formatting for JavaScript code?

Having trouble changing the CSS of elements that match b-video > p with an embed element using JQuery. Here's my code: $('div.b-video > p').has('embed').attr('style','display:block;'); Can anyone help me ...

The Access-Control-Allow-Origin CORS header is missing in React JS, resulting in a status code of 401

Having an issue with sending the access token in the header using Axios. I'm getting the following error: The request to 'http://192.168.0.254:3333/api/DOTMobileApi/test' from origin 'http://localhost:3000' has been blocked by COR ...

The background hue concealing the shadow of another box

I am facing an issue with my table styling where I want to apply a box-shadow effect when hovering over a row. However, despite setting the z-index accordingly, the background color of the td elements ends up covering the box-shadow, resulting in the hover ...

Is XPath applicable to data that has been labeled?

I am looking to access the data within the second div: <div class="my-3"> <div class="d-flex justify-content-between">Monthly:</div> <div>0 / 30,000</div> </div> Within the code above, I want to t ...

Insert the picture into an HTML document and retrieve the image file location

I successfully used <input type="file" accept="image/*"> to upload an image and then applied base64 encoding of the image in the onload callback function of a FileReader instance. However, I encountered a problem when trying to assign this base64 enc ...

Generating basic mesh shapes using three.js programmatically

Is there a way to easily generate basic shapes such as ramps, cones, and revolution shapes using only their vertices in three.js? ...

Error encountered in jQuery Datatables: aData variable is not defined

I am having an issue with a jQuery datatable that is using an Ajax data source. This is how the table is set up: $("#tblNotes").DataTable({ "ajax" : { "url": "/projects/ajaxGetProjectNotes/", "type" : "post", ...

DataGrid Component: Implementing a Button for Deleting Rows

I am currently developing a React component with MaterialUI, incorporating a Datagrid to manage a queue of files. The layout resembles this: https://i.sstatic.net/oh0GH.png One functionality I'm focusing on is when the user clicks on the three dots a ...

Full height container with footer

Currently facing an issue with a page I am working on. Here is a snippet of the code: <div id="header"> // header.. </div> <div id="content"> // content where ajax is loaded (should cover at least 100% of the site height) <!- ...

ways to disrupt a timeout

I am attempting to incorporate a functionality similar to this example: https://www.w3schools.com/jsref/met_win_cleartimeout.asp On my personal website: If you enter certain characters in the input field, select one of them, and then pause for 5 seconds, ...

Displaying CSS image slideshow images side by side

Currently I am a student studying the fundamentals of HTML and CSS. My latest project involved creating a website showcasing my work from digital arts class, complete with a simple CSS slideshow. The original intention was to have each image displayed on i ...

Is there a way to remove deleted SASS styles from the generated CSS?

Is it possible to remove markups from a generated css file using sass? For instance, in my scss file I have: body{ background:$dark; } The resulting CSS file looks like this: body{ background: #000; } If I delete that line of code in sass, what will h ...

Utilizing Cookies in an AJAX Request for Cross-Domain Communication with Pure Javascript

I am in the process of developing an Analytics application and I'm seeking a method to uniquely identify each user's device. Currently, my approach involves generating a "cookie" from the server side to track all page clicks and interactions thro ...

Is it possible for an object to be undefined in NextJS Typescript even after using a guard

Hey there, I could really use some help with a React component I'm working on. This is one of my initial projects and I've encountered an issue involving fetching async data. Below is the current state of my solution: import { Component } from &q ...

Combining ExtJS Paging with Dropdown Selection

Having an issue with a combo box created using the ExtJS framework. The pagination feature is not working correctly. Can anyone provide assistance with this? var store = new Ext.data.ArrayStore({ fields: ['id'], pageSize:1, data : ...

Is it possible for a hybrid app using Angular 7/1.x to enable Hot Module Replacement (H

Having trouble implementing HMR on a hybrid Angular application using the downgradeModule strategy. I previously sought help from a similar question on Stack Overflow Can an Angular 5/1.x hybrid app support HMR? but didn't find a satisfactory answer. ...

Emphasize a specific portion of the text

While developing a project similar to Google Search using React.js and GoogleAPI, an issue arose. For instance, when searching "tea" on Google, the results display "Searches related to tea" at the bottom. The term "tea" appears in bold. How can this be imp ...

What is the correct method for accessing an array within an object that is nested inside an array within a JSON file in Angular?

In my Angular controller code, everything is functioning properly except for the $scope.Product. I am unable to access the array of product details. Here is the relevant code snippet: .controller('aboutCtrl', function ($scope, aboutService) { ...

Unable to utilize jQuery library in AngularJS partial HTML files, despite being loaded in the index.html file

I have been working with a web template that utilizes jQuery image gallery and other elements. Now, I am looking to convert this template into an AngularJS structure. To do so, I have created partial HTML pages like slider.html or contact.html along with t ...

Implementing a vertical divider line in between columns with Foundation CSS

Here is the HTML code snippet: <link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.0/css/foundation.min.css" rel="stylesheet"/> <div class="card"> <div class="card-section"> <section class="row"&g ...