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

98
Visualizações
p tag is removing new line character in string

I have a string that contains a new line character. For example:

const myString = `hello\nstring`

I am then trying to dispaly this string in a functional component, which is wrapped around a p tag. For example:

export default function App() {
  const testString = "hi\nString";
  return (
    <div className="App">
      <p>{testString}</p>
    </div>
  );
}

For some reason, the p tag simply displays "hello string" on one line when I'd like for it to be displayed across two lines. Is there a way that I can stop the p tag from removing the new-line character from within the string?

The following Code Sandbox contains the executable example: https://codesandbox.io/s/unruffled-allen-ehrsd8?file=/src/App.js

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

0

Did you use <br/> code to break the line.? Please check code as given below-

export default function App() {
  const testString = "hi<br/>String";
  return (
    <div className="App">
      <p>{testString}</p>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use .split method whenever \n is found inside your code to enter new line

  function NewlineText(props) {
  const text = props.text;
  const newText = text.split('\n').map(str => <p>{str}</p>);
  
  return newText;
}

export default function App() {
  const testString = `hi\nString`;
 return (
    <div className="App">
<p><NewlineText text={testString} /></p>
    </div>
  );
}

This site might clear your any remaining doubts.

about 4 years ago · Juan Pablo Isaza Relatório

0

I think this is because new lines and white spaces are collapsed in browser by default. You do not need any JavaScript for this, just add following line in your stylesheet -

p {
  white-space: pre-wrap;
}
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