How to set a class Name in React? (ie html class attribute)

About

In HTML, the attribute property of an element is class, but in React, the attribute key is className.

How to conditionally set a class

With the conditional statement

return (
  <div
    className={`Button-root ${disabled ? 'Mui-disabled' : ''} ${selected ? 'selected' : ''}`}
  />
);

With library

With the clsx or obj-str, you can build conditional className

Example with clsx, instead of writing conditional statements, you would write

import clsx from 'clsx';

return (
  <div
    className={clsx('uiButton-root', {
      'ui-disabled': disabled,
      'ui-selected': selected,
    })}
  />
);





Discover More
How to import Svg into React (Manually and with SvgR)?

This page shows you how you can show Svg in React. The problem is that Raw Svg are not natively React component and therefore cannot be added to the React tree directly. URL The Javascript world is...
React Props Chrome Plugin
React - Props

props is the only object argument passed from a component to another component. Props are similar to State, but is public and not fully controlled by the component. You can also pass arguments to...
What are React HTML elements? (Built-In)

In React, HTML elements are: built-in React elements that wraps a HTML element the leaf of the React DOM tree In Jsx A css class that we will apply to our HTML element The component with...



Share this page:
Follow us:
Task Runner