Implementing React with CSS styling is a popular way to provide

I am facing an issue with a reusable component that is supposed to handle two different states based on the use case. The problem arises when trying to apply CSS styles depending on which state is provided. For some reason, it only applies the styles from the "status" state and completely ignores the condition for the "isVerified" state. I am using Chakra UI.

type Props = {
  isVerified?: boolean;
  status?: "active" | "inactive" | undefined;
};


 export const TestIcon = ({
  status,
  isVerified,
}: Props): ReactElement => {
  return (
    <Icon
      backgroundColor={
        (status === "active" ? "green.50" : "dark.50") ||
        (isVerified ? "dark.800" : "dark.50")
      }
      borderRadius="full"
      boxSize="illustrationSize"
      fill={
        (status === "active" ? "green.500" : "dark.200") ||
        (isVerified ? "base.white" : "dark.200")
      }
    />
  );
};

Answer №1

I believe the reason for this behavior is because of the ternary operator:

status === "active" ? "green.50" : "dark.50"
. If the status argument is false, it will always default to the else statement. In my opinion, a better approach would be:

status === "active" ? "green.50" : isVerified ? "dark.800" :"dark.50"

Alternatively, you could implement this logic using an if-else structure based on your top priority:

if(status === "active") return "green.50";
if(isVerified) return "dark.800";
return  "dark.50"

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 best way to update state and add elements to an array?

This is my current state: class table extends Component { state = { arr: [], numofSub: 1 }; I am attempting to update the 'arr' state by pushing an object fetched from my database. The fetch operation returns data successfully as I ...

Typescript Guide: When should we explicitly assign the type of each property in a scenario where all properties are identical?

Imagine I have an object: interface Name { first: string; middle: string; last: string; blah: string; blahblah: string; } It's clear that each property is a string type. Is there a way to reduce repetitive typing of "string"? ...

No cookies are allowed with NextJS SSR Headers

Everything was smooth sailing on my development environment with this code - no issues, no bugs, it just worked. But when I deployed to production, it's like the cookies disappeared. The ctx.req.headers are there, but no cookie. This same code has bee ...

Error: Unable to access the 'selectPage' property since it is undefined

I am experiencing an issue with my code while using React, Material-UI, and lodash. Specifically, the render method is giving me an error stating "TypeError: Cannot read property 'selectPage' of undefined": tmppage = _.map(_.range(0, th ...

How can I access the marker's on-screen location in react-native-maps?

Looking to create a unique custom tooltip with a semi-transparent background that can overlay a map. The process involves drawing the MapView first, then upon pressing a marker on top of the MapView, an overlay with a background color of "#00000033" is dra ...

React instantly console logs all indices upon rendering

I am working on a React project where I need to render an array of images using the map function. My objective is to make these images clickable and log their corresponding index in the console for later use, such as setting the state. However, the curre ...

Importing CSS and JavaScript files into a JSP page in Spring MVC

After adding a CSS file to my JSP file, I encountered an issue that prompted me to search for solutions. Despite trying the tutorial from the following link, it did not work for me: My project was created using Spring Tool Suite and here is my file struct ...

How can I make either the left or right section remain sticky for a certain period of time?

Can someone help me figure out how to fix a specific section in place temporarily, similar to the example provided below? In the link above, the Apple Watch product stays fixed while the content on the right moves partially, eventually causing the entire ...

Overlaying images with cropped elements

When uploading an image on a webpage, I want to display a preview of the image. I am looking to create an overlay that showcases the image in a rectangle at the center, surrounded by a semi-transparent background outside the rectangle. Something resemblin ...

What is the best way to incorporate React Redux into a class-based component?

As a newcomer to Redux, I’m curious about whether it’s possible to utilize Redux in a class component. Since hooks are tailored for function components, I decided to export a function that employs useSelector to interact with the store: import { useSel ...

Tips for achieving full width design within a container

Hello everyone, I require some assistance. I am attempting to create a full-width navigation while being constrained by a wrapper div with a fixed width of 900. I am unsure of how to achieve this without creating an additional div like the header. I am see ...

Dealing with CORS Issues Despite Activation

Currently, I am in the process of developing a web application using React. The API calls are made through HTTP triggers in Azure function apps and all APIs are functioning correctly except for one issue during integration - CORS. Despite my attempts to re ...

Modify the text inside the <span> tag using the :hover and :hover:after pseudo-classes

This code is working properly: HTML: <span class="counter" >test</span> CSS: .counter:hover:after{ content: "hello"; } Result: testhello However, I am trying to replace 'test' with 'hello'. I attempted the foll ...

Creating a connection between a class Bill and a class SimplifiedBill: Best practices

As stated in the title, I am faced with a situation where I have two classes: SimplifiedBill, which includes only date and finalPayment, and Bill, which has the same properties as SimplifiedBill but also additional details such as taxes, user data, and ser ...

Error TS[2339]: Property does not exist on type '() => Promise<(Document<unknown, {}, IUser> & Omit<IUser & { _id: ObjectId; }, never>) | null>'

After defining the user schema, I have encountered an issue with TypeScript. The error message Property 'comparePassword' does not exist on type '() => Promise<(Document<unknown, {}, IUser> & Omit<IUser & { _id: Object ...

Challenge with Overriding Material-UI Themes

One of the challenges I faced was overriding the <ExpansionPanelSummary/> component in Material UI to reduce margin. To achieve this, I utilized a theme with overrides. const theme = createMuiTheme({ overrides: { MuiExpansionPanelSummary: { ...

"Utilizing Bootstrap to ensure content is aligned perfectly to the baseline

I've been struggling to align my contents to the baseline while working with the bootstrap framework. I tried following the instructions in the documentation, but it didn't work out as expected. <div class="row"> <div class="col-12 ...

What causes the inconsistency in time intervals in the loop?

My goal was to create a simple slider that loops through 3 images, with each image staying visible for 3 seconds before fading out and the next one fading in. However, I encountered an issue: The first image can stay for 3 seconds, but once the loop star ...

Can someone explain to me how this ternary operator works?

Can anyone demonstrate how to convert this function into a traditional if-else statement? export const orderArr = (arr: any[], key: string) => arr.sort((a, b) => ((a[key] > b[key]) ? 1 : (a[key] === b[key]) ? ((a[key] > b[key]) ? 1 : -1) : -1)) ...

Change from one value to another using a decaying sinusoidal wave

Can someone help me come up with a formula that will smoothly transition from a starting value to an end value over a specified time using a Sin or Cos wave? I'm attempting to replicate a bouncing effect like the one shown in my sample using CSS and ...