I have a parent and child components that I use to display some contents about Inventory reports using react props. basically, the props contain information about individual inventory report card and one of those props is a tag that could have a "weekly", "monthly" or "yearly" value. I would like to dynamically change the background of each tag based on the text. So for example. if the tag = "daily", the background of the tag element should be black, if tag="monthly" background should be red and if tag=yearly, the background should be able. Note that I only want to change the background of the tag element and I have a color scss file where I have stored different color variables whichIi want to use to accomplish the change mentioned above.
THE CARD COMPONENT CODE (child component)
<>
{props.details.map((value, index) => (
<div className='report-container' key={index}>
<div className="report-header">
<h1>{value.title}</h1>
<p className="date">{value.creation_date}</p>
</div>
<div className="tag-container">
<span className="tag"> {value.tag} </span>
</div>
<div className="report-card-deets">
<p className="file-size"> {value.file_size} </p>
<button>Download</button>
</div>
</div>
))}
;
</>
MY ARRAY containing values that will be populated in the card component
const ReportData = [
{
title: "Inventory Turnover report",
creation_date: "31-03-2023",
tag: "Daily",
file_size: "100 kb",
},
{
title: "Top ordered items report",
creation_date: "28-03-2023",
tag: "Yearly",
file_size: "203 kb",
},
{
title: "Stockout rate report",
creation_date: "01-04-2023",
tag: "Weekly",
file_size: "400 mb",
},
];
MY COLOR VARIABLES
$primary-color: #1F8A70;
$secondary-color: #68B984;
$tertiary-color: #D6DCD8;
$text-color: #000000;
$text-color-light: white;
$background-color: #EDE4E0;
$container-background: #F2E9E5;
$table-background: #1F8A70;
$card-gradient: linear-gradient(285.25deg, #92C1A2 30.98%, rgba(107, 175, 109, 0.43) 77.71%);
$background-gradient: conic-gradient(from 126.2deg at 50% 50%, #1F8A70 0deg, rgba(76, 167, 124, 0.54) 178.12deg, rgba(103, 184, 132, 0.278471) 295.36deg, #1F8A70 360deg);
I already tried inline conditioning but that is not recommended since I have 3 conditions rather than 2. Also, I was wondering if I would be able to do this with className but I haven’t quite figured that out at this moment. What would you recommend guys?
3
Answers
Changing the child component code to the following will fix the props value not showing:
<DynamicBackground className='tag' tag={value.tag} > {value.tag} </DynamicBackground>
You can use styled-component library to create components from html tags. In your case, this code would solve the problem:
First, you need create the styled-component:
This component can recive the tag prop and its ready to use in the CardComponent. It will looks like:
If you don’t want to use the Styled component as @Alejandro suggested, you can use this approach:
Note: I’m assuming that
Weekly
,Monthly
andYearly
are the only values thatvalue.tag
will hold, if it will hold any additional values like Daily, you will need to add it to the conditional part.