Ways to update the background hue for negative variables?

If the value of totalNetworkScore is negative, I want to update the background color of a styled div to red.

Answer №1

If you're looking to assign conditional classes, consider using a ternary operator.

:class="{{totalNetworkScore ? 'green' : 'red'}}"

For styling in css:

.green {
    background: green;
}

.red {
    background: red;
}

Answer №2

To achieve this effect, consider utilizing the @emotion/core library with the following approach:

import { css } from "@emotion/core";
import React from "react";

checkNegative = counter => {
  if (counter) {
    return css`
      background: green;
    `;
  }
};

class CustomComponent extends React.Component {
  state = { counter: -1 };
  render() {
    return <div css={checkNegative(this.state.counter)}>some text</div>;
  }
}

export default CustomComponent;

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

What is the method for adding two elements to an array?

Currently, I am using JavaScript to push elements into an array. As a result, the output looks like this: ["api", "management", "provision", "tenant", "external", "provider"] => Correct Now, after pushing data into the array, I want to append two el ...

What steps can I take to ensure that Bootstrap components are compatible with my mobile device?

[![This is the result I see when accessing my page on a mobile device <head> <title>Login Page</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=devic ...

The alignment of my card icons changes depending on the size of the paragraph

Here are my cards I am attempting to give them a relative position and the parent div an absolute position, but it is not working as expected. Despite several attempts, I still cannot get the desired result of positioning the cards correctly within the p ...

The web browser is failing to retain the session cookie after a React XHR request made through express-sessions with the latest

I'm currently using a React frontend to connect to a nodejs server running express-session. The frontend is hosted on localhost:3000, while the server runs on localhost:5000. Everything functions as expected when I use Postman from localhost (the ses ...

Is there a way to incorporate text at the end of a row in HTML?

I have a photo and some text on my website. The photo is displayed on the left side, while the text appears nicely on the right side. Now, I am looking to include an additional block of text below the existing text. How can I accomplish this? What steps ...

Are there any reliable charting APIs available that can efficiently handle the lazy-loading of large data sets using Ajax while scrolling or zooming?

Hey there! I need help finding a JavaScript charting API that allows me to query the server using specific bounds on the horizontal and vertical axes in order to retrieve new data points. Just to give you an idea, the dataset I'm working with is incre ...

Ways to ensure a ul li element perfectly fits inside a div element without any spaces

As a newcomer to CSS and HTML, I'm facing an issue with my ul li dropdown menu and login boxes. The background colors are red and blue, but there are gaps within the div that I want to fill with the height of the div. Below is the code snippet: @key ...

redux reducer returns an empty array after applying filter to the state

In my React component, I am utilizing Redux to manage the state. Since I am new to Redux, I have encountered some challenges with one of the reducers I created. One of the methods in my component involves making an HTTP request and then dispatching the dat ...

Bootstrap is an outdated page template that lacks responsiveness

My code below features a page designed using Bootstrap HTML and CSS, but it doesn't look right on all devices. For example, on mobile devices, the grid appears zoomed in and unresponsive. I tried removing grid-template-columns: auto auto;, which fixed ...

Guide on integrating a php script into a react application

I'm currently in the process of setting up a functional contact form on my website by following the guidance provided in this article link. However, the tutorial includes a php script to manage the post data, and I am using create-react-app and unsure ...

How to automatically open JQuery Accordion panels when the page loads

My Accordion loads with the 1st panel open upon page load, but I am looking for a way to have the entire Accordion closed by default. Any help or suggestions on how to achieve this would be highly appreciated. Thank you for your assistance. FIDDLE http:// ...

Styling elements with CSS to display inline blocks next to each other while taking up the full

I am facing a challenge with two text blocks of varying lengths. The left block contains short text while the right block may have lengthy content. Both blocks need to be displayed side by side, occupying 100% of the parent's width exactly. For a sim ...

Tips for creating a Bootstrap 5 navbar dropdown menu that opens on hover for desktop users and on click for mobile users

Just starting out in the world of Web Development, so any help would be greatly appreciated! I've been working on a Wordpress website for a company and encountered an issue with my CSS while implementing the Bootstrap 5 navbar. On desktop, the dropdo ...

Combining left-aligned and centered elements within a grid using flexbox

Looking to create a fluid responsive grid layout using flexbox, without the need for media queries. The number of elements in the grid can vary and each item should have a fixed, equal width with left alignment. The entire group should have equal left and ...

Injecting dynamic content using JavaScript

My goal is to develop a web page where clicking on any of the three images will cause the content to be inserted into the empty div located above the image links. Below is the JavaScript code I'm using: <script type="text/javascript" src="js/jque ...

Obtaining the previous value in DesktopDatePicker's onChange event

In my custom function within the onChange event, I am attempting to retrieve the updated date. However, there is an issue where the handleDatesChanged() function captures the previous date instead of the current one. <DesktopDatePicker ...

Harnessing the power of Meteor and Semantic-UI-React: Bringing attention to

I am currently developing an application using Meteor, React, and Semantic-UI. Despite being new to these frameworks/libraries, I have gone through the documentation multiple times but I am still struggling to figure out how to set focus on a particular co ...

Angular 10: Unexpected Behavior with Observables

When I initially call addPost(), the observable behaves as expected with a 5-second delay. However, when I call it a second time, the data updates in the HTML without any delay. On the other hand, the same observable with deletePost() functions perfectly. ...

Distinguishing Js versus Jsx

While working on React JS, a question came to mind. In the code below, both JavaScript (JS) and JSX are used, and they seem to respond similarly. So, why should we specifically use JSX? After searching online, I found information about the differences bet ...

We encountered non-serializable values within the navigation state due to passing a function as a parameter

My problem involves two specific screens: Screen A import React, { useState } from "react"; import { Text, View, Button } from "react-native"; const ViewA = ({ navigation }) => { const [val, setVal] = useState(null); const [val ...