Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

0

You can do it inline

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

0

objects.map((obj) => <div className={obj.name.title === 'Ms' ? 'classA' : 'classB'}>Ms</div>)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda