Rendering empty space in React
You could use <span> </span>
:
class Blinker extends React.Component {
constructor(props) {
super();
this.state = {
appear: true
}
this.blinker = this.blinker.bind(this);
}
blinker() {
this.setState({appear: !this.state.appear });
}
componentDidMount() {
setInterval(this.blinker, 1000)
}
componentWillUnmount() {
clearInterval(this.blinker);
}
render() {
const name = "testing";
const underScore = "_";
const com = "com";
return (
<div>
<div id="test"> { name } </div>
<div id="test">
{ (this.state.appear) ? underScore : <span> </span>}
</div>
<div id="test"> { com } </div>
</div>
);
}
}
ReactDOM.render(<Blinker />, document.getElementById('app'));
#test {
display: inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="app"></div>
It is because the three nested lines get transpiled as individual children of the parent div element, without taking the spaces into account. Solution is putting a space explicitly between the elements:
<div>
<div id="test"> { name } </div>
{''}
<div id="test">
{ (this.state.appear) ? underScore : "\u00a0\u00a0"}
{''}
</div>
<div id="test"> { com } </div>
</div>
class Blinker extends React.Component {
constructor(props) {
super();
this.state = {
appear: true
}
this.blinker = this.blinker.bind(this);
}
blinker() {
this.setState({appear: !this.state.appear });
}
componentDidMount() {
setInterval(this.blinker, 1000)
}
componentWillUnmount() {
clearInterval(this.blinker);
}
render() {
const name = "testing";
const underScore = "_";
const com = "com";
return (
<div>
<div id="test"> { name } </div>
<div id="test">
{ (this.state.appear) ? underScore : "\u00a0\u00a0"}
</div>
<div id="test"> { com } </div>
</div>
);
}
}
ReactDOM.render(<Blinker />, app);
#test {
display: inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="app"></div>