I'm reading Full Stack React The Complete Guide And came across this code snippet:
render() {
if(this.props.timerIsRunning){
return(
<div
className='ui bottom attached red basic button'
onClick={this.props.onStopClick}
>
Stop
</div>
)
} else {
return(
<div
className='ui bottom attached green basic button'
onClick={this.props.onStartClick}
>
Start
</div>
)
}
}
It uses a if statement to determine what will be rendered by the component, either a stop, or start button for a stopwatch. This example got me wondering if there was a way to do that using less space. Is there a way to create the div and then add certain properties and classes to that div depending on the value of this.props.timerIsRunning? If this weren't JSX I would propose something like this:
<div id=timerbutton></div>
<script>
let timerButtonDiv = document.getElementById(timerbutton)
if(this.props.timerIsRunning) {
timerbuttonDiv.className = 'ui bottom attached red basic
button'
timerbuttonDiv.innerHTML = Stop
} else {
timerbuttonDiv.className = 'ui bottom attached green basic
button'
timerbuttonDiv.innerHTML = Start
}
</script>
So... let this example speak to my lack of JSX understanding, but I guess, I am just wondering if (coming from an Angular 1.X background) there is a React/JSX equivalent of ng-show or ng-class. More broadly, I am wondering where the lines are between virtual DOM and actual DOM as far as standard DOM manipulation is concerned. any resources on how React transposes JSX would certainly be helpful.