ReactJS render string with non-breaking spaces
Solution 1:
Instead of using the
HTML entity, you can use the Unicode character which
refers to (U+00A0 NON-BREAKING SPACE):
<div>{myValue.replace(/ /g, "\u00a0")}</div>
Solution 2:
I know this is an old question, and this doesn't exactly do what you asked for, but rather than editing the string, what you want to achieve is probably be solved better using the CSS white-space: nowrap
attribute:
In html:
<div style="white-space: nowrap">This won't break lines</div>
In react:
<div style={{ whiteSpace: 'nowrap' }}>{myValue}</div>