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

99
Visualizações
LineBreak with HTML Tag

How can I split a string at its line breaks.

The Code below works fine. But I am looking for different solution without using "dangerouslySetInnerHTML".

Maybe someone knows.

<div> <div dangerouslySetInnerHTML={{__html: String.split("\n").join("<br />")}} </div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use CSS to use white-space: pre;, which will respect the line breaks in the text, within the targetted element.

.respect-line-breaks {
  border: 1px solid #444;
  white-space: pre;
}
<div class="respect-line-breaks">This
is
spread
over
multiple
lines
</div>

Is an example.

You can also use the HTML <pre> tag, instead of a <div> if you'd prefer, which negates the need for the CSS. <pre> is also supported in much older browsers too, if that's something you need.

One thing to be aware of though is that if later on when developing your application, you loop through all of your <div> elements, it won't pick up the <pre> elements, which is why I would personally use the CSS solution, if at all possible.

This also means that you can use something like a <p> tag, instead of a <div>, and keep all your normal text styling, etc., in place.

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