简体   繁体   中英

How do I add a new line in a react usestate string

Currently my code is something like this:

 const [simulationResult, setSimulationResult] = useState(""); let simulationFormatedString = "one /n two"; setSimulationResult(simulationFormatedString); <div> {simulationResult} </div>
I want to show the result as: one two

instead of:

one

two

as @dandavis said, line break is \\n not /n

Then, you can remove it by replacing it with zero space ""

const [simulationResult, setSimulationResult] = useState("");

const simulationFormatedString = "one \n two";
const finalSimulationString = simulationFormatedString.replace(/\r?\n|\r/g, "");
setSimulationResult(<span>{finalSimulationString}</span>);

<div>
{simulationResult}
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM