Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to use two classname in ReactJS

Introduction to ReactJS

Hello, budding programmers! Today, we're going to explore the world of ReactJS, a popular JavaScript library for building user interfaces. Imagine this library as a box of Lego blocks. Just as you can build various structures using different Lego blocks, ReactJS allows you to build dynamic web applications using different components.

Understanding Components in ReactJS

Before we dive into the main topic, let's take a moment to understand what components are. In ReactJS, components are individual pieces of your application's UI, just like how a Lego piece is a component of the whole Lego structure. They are reusable and can be nested inside other components to build complex user interfaces.

Classnames in ReactJS

Now, let's talk about classnames. In ReactJS, a classname is like a label you put on a component. This label (or classname) allows us to apply specific CSS styles to the component.

In traditional HTML and CSS, we use the term "class" to label elements, but in ReactJS, we use the term "classname." This is because "class" is a reserved keyword in JavaScript.

Using Multiple Classnames in ReactJS

Let's say we have two Lego blocks of different colors. We want to label them both as "block" but also label them according to their colors. Similarly, in ReactJS, we might want to apply more than one classname to a component.

Applying multiple classnames in ReactJS is straightforward. Here's an example:

import React from 'react';

function App() {
  return (
    <div className="App">
      <header className="App-header App-intro">
        <h1>Multiple Classnames in ReactJS</h1>
      </header>
    </div>
  );
}

export default App;

In the above code, the header component has two classnames: "App-header" and "App-intro". These classnames are separated by a space within the quotation marks.

Why Use Multiple Classnames?

You might be asking, "Why would I want to use multiple classnames?" Well, each classname can have its own set of styles, and applying multiple classnames to a single component allows you to combine these styles.

Think about dressing up a doll. You might put on a shirt, pants, and a hat, which all have different styles. Similarly, each classname adds a layer of style to your component.

Using Classnames Module for Multiple Classnames

While using a space-separated list of classnames is simple and effective, it can get messy when you have a lot of classnames or need to conditionally apply classnames. In such cases, a third-party library named Classnames comes in handy.

The Classnames library allows us to bind classnames in a cleaner way. Here's how to install it:

npm install classnames

Once installed, you can use it in your ReactJS file like so:

import React from 'react';
import classNames from 'classnames';

function App() {
  let buttonClasses = classNames('Button', 'Button--large', 'Button--blue');

  return (
    <div className="App">
      <button className={buttonClasses}>Click me!</button>
    </div>
  );
}

export default App;

In the above code, buttonClasses is a string that combines three classnames: 'Button', 'Button--large', and 'Button--blue'. This string is then used as the classname for the button component.

Conditional Classnames

Classnames library also allows us to conditionally apply classnames. For instance, imagine you have a button that should have a "Button--active" classname only when it's clicked. Here's how you can achieve this:

import React, { useState } from 'react';
import classNames from 'classnames';

function App() {
  const [isActive, setIsActive] = useState(false);

  let buttonClasses = classNames('Button', 'Button--large', { 'Button--active': isActive });

  return (
    <div className="App">
      <button 
        className={buttonClasses} 
        onClick={() => setIsActive(!isActive)}>
        Click me!
      </button>
    </div>
  );
}

export default App;

In this code, the 'Button--active' classname is only applied if the isActive state is true. When the button is clicked, the isActive state toggles, thereby toggling the 'Button--active' classname.

Wrapping Up

By now, you should have a good understanding of how to use multiple classnames in ReactJS. Remember, classnames are like labels for your components, and they allow you to apply specific styles to your components. You can use multiple classnames by listing them within the quotation marks, separated by spaces. For a cleaner approach, especially when dealing with many or conditional classnames, you can use the Classnames library.

Remember the Lego analogy we started with? Just as you can attach different labels to Lego blocks (like color, size, or shape) and create a diverse structure, you can apply multiple classnames to ReactJS components and build complex, dynamic user interfaces.

So, go ahead, give it a try! Build something awesome with ReactJS and multiple classnames. Happy coding!