Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
Changing table row colour based on values from map function ( React Js)

i am trying to figure out a way to change the table row colour based on the certain values inside the map function. I have been trying different solutions like UseRef hook, other stack overflow solutions etc, but was not successful.

         {data.map((item, i) => (
                
                    <Tablerows key={i} >
                    <Tabledata>{item.name}</Tabledata>
                    <Tabledata>{item.place}</Tabledata>               
                    <Tabledata>{dateconvert(item.LOGTIME)}</Tabledata>
                    </Tablerows>
                ))}

Now i call a dateconvert function in the third column to convert to unix time to normal time

function dateconvert(time_1){
  var date1      = new Date(time_1*1000);
  var convert_1  =('0' + date1.getDate()).slice(-2)      + '/'
           + ('0' + (date1.getMonth()+1)).slice(-2) + '/'
           + date1.getFullYear()                    + ' \xa0\xa0\ '
           + ('0' + date1.getHours()).slice(-2)     + ':'
           + ('0' + date1.getMinutes()).slice(-2)   + ':' 
           + ('0' + date1.getSeconds()).slice(-2);

  return convert_1
  }

Everything works fine until now !!. And i have the desired output.

Question :

i want the table row to have a red colour under the following condition. (here "date" represents my item.LOGTIME)

  if((Date.now() - date) /(60 * 1000) >11){ 
     
//change colour of the row to red
    }

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think this is how you can get this working.

const data = [
{ name: "brij1", place: "place1", LogTime: "I ignored this for now" },
{ name: "brij2", place: "place2", LogTime: "I ignored this for now" },
];

const DecoratedContainerData = ({ children }) => {
  const styleProps = {
    style: {
      color: "red",
    },
    // any other props here...
  };
  return children(styleProps);
};

const conditionallyDecorateContainerData = (inputDate) => {
  const convertedDate = dateconvert(inputDate);
  const calculatenumber = 11
    //(Date.now() - Date.parse(convertedDate)) / (60 * 1000);
    // Replace 11 with your formula (I have hardcoded this just to check the color style is working. you can also try by replacing 11 with lower or higher number)

  console.log(calculatenumber);
  if (calculatenumber > 11) {
    return (
      <DecoratedContainerData>
        {(props) => <div {...props}>{convertedDate}</div>}
      </DecoratedContainerData>
    );
  }
  return <div>{convertedDate}</div>;
};

{data.map((item, i) => (
  <div key={i}>
    <div>{item.name}</div>
    <div>{item.place}</div>
    {/* One way */}
    <DecoratedContainerData>
      {(props) => <div {...props}>{dateconvert(item.LOGTIME)}</div>}
    </DecoratedContainerData>
    {/* Second Way */}
    {conditionallyDecorateContainerData(item.LOGTIME)}
  </div>
))}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!