← React Beginner Questions - Set 7 Q62

How do you handle dynamic class names in React?

React Beginner Questions - Set 7

Example

function Badge({ tone, active }) {
  const cls = `badge badge--${tone}${active ? ' badge--active' : ''}`;
  return <span className={cls}>New</span>;
}

// or with an array, which reads better with several conditions
const cls = ['badge', `badge--${tone}`, active && 'badge--active']
  .filter(Boolean).join(' ');

What to notice

← Back to the question list