Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  138] [ 4]  / answers: 1 / hits: 101727  / 8 Years ago, mon, march 28, 2016, 12:00:00

Using Facebook React.
In a settings page, I have a multiline textarea where a user can enter multiline text (in my case, an address).



<textarea value={address} />


When I try to display the address, so something like {address}, it doesn't show the line breaks and is all on one line.



<p>{address}</p>


Any ideas how to solve this?


More From » reactjs

 Answers
6

There's no reason to use JS. You can easily tell the browser how to handle newline using the white-space CSS property:


white-space: pre-line;


pre-line


Sequences of whitespace are collapsed. Lines are broken at
newline characters, at <br>, and as necessary to fill line boxes.



Check out this demo:




<style>
#p_wrap {
white-space: pre-line;
}
</style>

<textarea id=textarea></textarea>
<p id=p_standard></p>
<hr>
<p id=p_wrap></p>
<script>
textarea.addEventListener('keypress', function(e) {
p_standard.textContent = e.target.value
p_wrap.textContent = e.target.value
})
</script>




browser


[#62787] Thursday, March 24, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alejandro

Total Points: 231
Total Questions: 102
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
alejandro questions
Mon, Jul 18, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Thu, Sep 10, 20, 00:00, 4 Years ago
;