2

I want to format some text in a React app, and for some reason, the whitespace is getting removed. For example, if I have I class with the render method

render() {

    return (<div>
               <div>{"average time: " + 5}</div>
               <div>{"best    time: " + 1}</div>             
            </div>);
}

then the rendered output removes the spaces I have in the second line, yielding an output that looks like

average time: 5
best time: 1

However, I want the output to look like

average time: 5
best    time: 1

How do I achieve that?

Elsewhere, I read that adding a className with display: block would fix this, but that did not help. Specifically, I tried this:

render() {

    return (<div>
               <div className="stats_entry">{"average time: " + 5}</div>
               <div className="stats_entry">{"best    time: " + 1}</div>             
            </div>);
}

where the css file has

.stats_entry {
  display: block;
}

EDIT:

Per suggestion, I tried changing the css to

.stats_entry {
  white-space: pre;
}

but this did not fix the issue.

EDIT:

wrapping the text with pre solved the issue:

render() {

    return (<div>
               <div><pre>{"average time: " + 5}</pre></div>
               <div><pre>{"best    time: " + 1}</pre></div>             
            </div>);
}
bremen_matt
  • 6,347
  • 4
  • 38
  • 78
  • 1
    This has nothing to do with **React**, it's basic **HTML** – vsync Dec 20 '18 at 09:38
  • Ok. What is the solution? – bremen_matt Dec 20 '18 at 09:40
  • The solution is to [google](https://www.google.com/search?ei=xGMbXKa4MMm91fAPudiB2AE&q=html+multiple+spaces&oq=html+multiple+psaces&gs_l=psy-ab.3...0.0..5038...0.0..0.0.0.......0......gws-wiz.zBLw6FWXQ6k) before asking – vsync Dec 20 '18 at 09:40
  • I did google, as I said in the original question. – bremen_matt Dec 20 '18 at 09:41
  • Then why don't you use ` ` if you did Google as you say? – vsync Dec 20 '18 at 09:47
  • Because I did not realize this was an html issue. – bremen_matt Dec 20 '18 at 09:48
  • In the future if you come across a weird scenario, try to reproduce it in the most simple env possible (HTML, CSS) and then gradually make things more complex, or the other way, start with what you have and then start to remove things until you focus on the origin of the problem. – vsync Dec 20 '18 at 10:00

1 Answers1

2

You should use white-space: pre; in your css

Doğancan Arabacı
  • 3,817
  • 2
  • 15
  • 25
  • This did not fix the issue. – bremen_matt Dec 20 '18 at 09:39
  • 1
    This should be a comment and not an answer. it's also not the correct answer since it does not explain why this is happening and how to properly solve it without hacks. – vsync Dec 20 '18 at 09:40
  • idk why @vsync is mad at this answer, this is exactly what I use StackOverflow for, quick solutions to my silly problems. Anyway, this was precisely what I needed. – Tushar Sadhwani Oct 30 '20 at 11:58