Skip to main content
InterviewHubPRO
Section Overview
Q59

Question 59 of 407

General

14%
React Core & Hooks (407 Qs)•General

How to conditionally apply class attributes?

mid LevelCode ExamplereactGeneral

You shouldn't use curly braces inside quotes because it is going to be evaluated as a string.

jsx
<div className="btn-panel {this.props.visible ? 'show' : 'hidden'}">

Instead you need to move curly braces outside (don't forget to include spaces between class names):

jsx
<div className={'btn-panel ' + (this.props.visible ? 'show' : 'hidden')}>

Template strings will also work:

jsx
<div className={`btn-panel ${this.props.visible ? 'show' : 'hidden'}`}>