Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

304
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda