Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change the color dynamically in reactjs?

I am working in react project and i want to change the color of my present and absent data which is fetched from database.

For this purpose i used condtional statememt but i got all green in color. please suggest me how can i do it or where i am doing it wrong. as there are number of soultion in SO but none of them worked for me

import React, { useEffect, useState } from 'react'
import { NavLink, useParams } from 'react-router-dom'
import Navbar from './Navbar';
import { useNavigate } from 'react-router-dom';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';


const Contracter = () => {
    
 
    const [contarctor, setcontarctor] = useState([])
   
    const color = contarctor[0]?.attstatus === "present" ? 'green' : 'red';
    const styles = {  backgroundColor: color };

 const getmemberid = async (PM_id) => {

        try {

            const res = await fetch(`/getattendance/${PM_id}`, {
                method: "GET",
                headers: {
                    Accept: "application/json",
                    "Content-Type": "application/json"
                },
                credentials: "include"
            })

            const result = await res.json()
            setcontarctor(result)
            console.log();

        } catch (error) {
            console.log(error);
        }
    }






    return (
<>
                            {
                                contarctor?.map((item, i) => {
                                    return (
                                        <tbody>
                                            <tr>
                                                <th scope="row">{item.attdate}</th>
                                                <td><span style={styles} class="badge rounded-pill ">{item.attstatus} </span> <i onClick={() => modalClick(item.atte_id)} data-bs-toggle="modal" data-bs-target="#exampleModal" class="bi bi-pencil-square "></i> </td>


                                            </tr>
                                        </tbody>
                                    )
                                })
                                                  
        </>
    )
}

export default Contractor

enter image description here
this is my output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

const color = contarctor[0]?.attstatus === "present" ? 'green' : 'red';
const styles = {  backgroundColor: color };

Only checks the first ([0]) index and applies that for all the items.

Remove that, and just set style do it inline in the map():

style={{ backgroundColor: item.attstatus === "present" ? 'green' : 'red' }}

So the final JSX:

{
    contarctor?.map((item, i) => {
        return (
            <tbody>
                <tr>
                    <th scope="row">{item.attdate}</th>
                    <td>
                        <span style={{ backgroundColor: item.attstatus === "present" ? 'green' : 'red' }} class="badge rounded-pill ">{item.attstatus} </span> 
                        <i onClick={() => modalClick(item.atte_id)} data-bs-toggle="modal" data-bs-target="#exampleModal" class="bi bi-pencil-square "></i> 
                    </td>
                </tr>
            </tbody>
        )
    })
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading