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

272
Visualizações
Combine conditional with static class NextJS

In NextJS I'm trying to apply both a static CSS-class and a conditional class to an element. Separated from each other I can make both work, but when combining them it will result in an unexpected error.

# This will work
<span className="font-medium">{message}</span>

# This will work too
<span className={status ? "bg-green-600":"bg-orange-600"}>{message}</span>

In Visual Studio the following snippet will give an error ',' expected.ts(1005)

# This (where I'm trying to apply *both* classes to the same element) won't work
<span className={"font-medium" + {status ? "bg-green-600":"bg-orange-600"}}>{message}</span>

Ignoring that will give following error:

Error: 
  x An object member cannot be declared optional
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

You need a space after font-medium, because it will be interpreted as a single class otherwise

<span className={"font-medium " + status ? "bg-green-600":"bg-orange-600"}>{message}</span>

And with template literals :

<span className={'font-medium ${status ? "bg-green-600" : "bg-orange-600" }'}>{message}</span>

(Please note it's `and not ' here)

In this type of cases, dont forget to debug by inspecting the element and checking the classes

about 4 years ago · Santiago Gelvez Relatório

0

You can also use backtick (`) as an alternative like this

<span className=`font-medium ${status ? 'bg-green-600' : 'bg-orange-600' `}>{message}</span>
about 4 years ago · Santiago Gelvez 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