Skip to article
detaID ↗
← Back home

Managing ClassName with clsx in React

Learn how to use clsx to manage className dynamically in React more cleanly and efficiently.

2 min read
#clsx#React#CSS

When building a React application, we often need to determine className dynamically based on certain conditions. The clsx library simplifies this process and makes the code cleaner and more readable.

Installation

To use it, first install it with npm, yarn, or pnpm:

sh
npm install clsx # or yarn add clsx # or pnpm add clsx

You can learn more about clsx on its official GitHub repository: clsx on GitHub

How to Use

With clsx, we can manage class names conditionally without writing long and cluttered code.

Basic Example

tsx
import clsx from "clsx"; const Button = ({ primary }: { primary: boolean }) => { return ( <button className={clsx("btn", { "btn-primary": primary, "btn-secondary": !primary, })} > Click me </button> ); };

In this example:

  • If primary is true, the className becomes "btn btn-primary".
  • If primary is false, the className becomes "btn btn-secondary".

Combining Multiple Classes

You can also combine classes from various conditions:

tsx
const isActive = true; const isDisabled = false; const className = clsx( "base-class", isActive && "active-class", isDisabled ? "disabled-class" : "enabled-class" ); console.log(className); // Output: "base-class active-class enabled-class"

Using clsx with Tailwind CSS

If you are using Tailwind CSS, it is recommended to use tailwind-merge alongside clsx to avoid conflicting classes:

tsx
import { twMerge } from "tailwind-merge"; import clsx from "clsx"; const buttonClass = twMerge( clsx("p-4 text-white", isActive && "bg-blue-500", isDisabled && "opacity-50") ); <button className={buttonClass}>Click me</button>;

With twMerge, conflicting classes (such as p-2 and p-4) are filtered so that only the most relevant class is applied.

You can learn more about tailwind-merge here: tailwind-merge on GitHub

Conclusion

Using clsx helps make className management cleaner and more flexible. If you use Tailwind CSS, tailwind-merge is highly recommended to ensure that classes do not conflict. With this approach, your React code becomes more organized and easier to maintain.

More writing

A few more notes from my archive.

Fixing the 'Ghost Changes' Bug in Bulk Edit Tables

Deep dive into debugging a tricky state management issue where the system detected changes that didn't actually exist, and how comparing with original values solved the problem.

Conditional Rendering: Keep It Inside the Component

Best practice for placing conditional rendering logic within React components using context or internal state.

Build a State Machine with XState and Use It Across React Components (TypeScript)

Learn how to build a state machine with XState and use it across React components in TypeScript.

React Grid Layout Widget DnD and Resizable Example

This post demonstrates how to build a React grid layout with draggable and resizable widget components using the react-grid-layout library. We'll put together a simple dashboard where users can rearrange widgets however they like.

Leave a comment