I am currently creating a next JS application and using react states in that. I want to add a line break in a string value of the state. I tried multiple things like these but none of them work:
setState("a n b");
setState("a <br> b");
setState("a {n} b");
setState("a {'n'} b");
Can someone please help with this
Edit: Sorry for such a vague question. Let me provide a little more context as to what my code currently looks like.
const [ state, setState ] = useState('abc')
const handleSubmit = async (e) =>{
setState('a');
if(//some condition){
setState(state + '<br />b')
}else{
setState(state + '<br />c')
}
}
This is basically how I want my code to be. Any suggestions. Thanks
2
Answers
You need to create a JSX element with
<br />
, and as string, if you want React to convert it to the correct DOM.Use a fragment –
<Fragment>{prev state} <br /> b</Fragment>
, or the shorthand version (not supported in SO snippets) –<>a <br /> b</>
.I see 2 options here:
<pre/>
to display your text