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

137
Views
How to conditional render a class in .map with react/typescript

I have mapped and filtered through this object and I want to conditionally render a class based off the text that is outputted. In this case I want it to be the text "Ms". Is there a way of doing this within the .mapped object?

I've tried using regex. I've tried queryselecterAll. I've tried for each loop.

I feel like there is some logic im missing here.

This is mapped object

const [data, setData] = useState(false);
  const [searchUser, setSearchUser] = useState("");

useEffect(() => {
    setLoading(true);

    const fetchList = async (): Promise<UsersList> => {
      const result: any = await fetch("https://randomuser.me/api/?results=20")
        .then((res) => res.json())
        .then((data) => {
          setData(data);
          setLoading(false);
          console.log(data);
        });

      return result;
    };
    fetchList();
  }, []);

the class

.title {
background-color: blue;

}

mapped object

{data?.results
            .filter((val: any) => {
              if (searchUser === "") {
                return val;
              } else if (
                val.name.first.toLowerCase().includes(searchUser.toLowerCase())
              ) {
                return val;
              } else if (
                val.email.toLowerCase().includes(searchUser.toLowerCase())
              ) {
                return val;
              } else if (
                val.location.city
                  .toLowerCase()
                  .includes(searchUser.toLowerCase())
              ) {
                return val;
              }
            })
            .map((d: any) => 

            (
              <div
                style={hoverStyle}
                className="text-blue-900 ease-in-out delay-150 hover:-translate-y-1 hover:scale-110 duration-300  border-white shadow-slate-500 bg-white hover:opacity-95 rounded-lg container p-5"
              >
                <img
                  className="rounded-full w-44 h-44 "
                  src={d.picture.large}
                />
                <h2 className="">{d.name.title}</h2>
    
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can do it inline

style={{background-color: d.name.title==='Ms' ? "blue" : "white" }}
about 4 years ago · Juan Pablo Isaza Report

0

objects.map((obj) => <div className={obj.name.title === 'Ms' ? 'classA' : 'classB'}>Ms</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!