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

314
Visualizações
Add <br> tag using replaceAll() in javascript

I have this string:

export default function App() {
  const string = 'test data,cars,colors,demo';

  return (
    <div className="App">
      <h1>Hello {string.replaceAll(',','<br>')}</h1>
    </div>
  );
}

I expect:

test data<br>cars<br>colors<br>demo

But i get one string without breack inside my string. How to achieve what i expect using replaceAll()?

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

0

In order to display html from string you have to use dangerouslySetInnerHTML

export default function App() {
  const string = 'test data,cars,colors,demo';

  return (
    <div className="App">
      <h1 dangerouslySetInnerHTML={{ __html: `Hello ${string.replaceAll(',','<br>')}`}}></h1>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming this is a React application you need to use dangerouslySetInnerHTML to add the new HTML to the page.

function Example() {

  const string = 'test data,cars,colors,demo';
  const html = `Hello ${string.replaceAll(',', '<br>')}</h1>`;

  return (
    <div className="App">
      <h1 dangerouslySetInnerHTML={{ __html: html }} />
    </div>
  );

}

// Render it
ReactDOM.render(
  <Example />,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

I don't suggest you dangerouslySetInnerHTML. It introduces both security and performance issues, and even if you are working on learning project, you better avoid such an approach.

The reason why your code does not work, JSX does not magically convert string "<br>" into appropriate element (until there is dangerouslySetInnerHTML, sure).

But JSX can render arrays just fine. It allows us to split initial string into elements: string.split(', ') and then inject JSX's <br /> with the help of .flatMap()(think about it as if .join() could return array with additional elements in between elements of source array).

{
  string.
    split(', ').
    flatMap((el, index) => index ? [<br />, el]: el)
}

This approach is way more powerful than dangerouslySetInnerHTML since instead of simple BR you may use any JSX tree with React custom components, context and event handlers.

Another approach is to replace ', ' with newlines and apply CSS style white-space: pre-wrap(check white-space docs on all values available)

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