Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to pass a class name in ReactJS

Understanding Class Names in ReactJS

What are Class Names?

Before we dive into the thick of things, let's understand what class names are. Think of a class name as a unique label you stick on an element in your webpage. It's like giving your pet a name, so you can call them when you need them. In the world of web development, class names are essential as they allow us to target specific elements and apply styles to them.

The Importance of Class Names in ReactJS

Unlike regular HTML, where we use the class attribute, ReactJS uses className to assign class names to JSX elements. The reason behind this is that class is a reserved keyword in JavaScript, and JSX is closer to JavaScript than HTML.

Here's an example of how to use className in a React component:

function HelloWorld() {
  return <h1 className="greeting">Hello, world!</h1>;
}

In the example above, we have a HelloWorld function that returns a heading with the class name greeting. This allows us to apply specific styles to the HelloWorld component using the .greeting selector in our CSS.

How to Pass a Class Name in ReactJS

Now, let's talk about how you can pass a class name to a ReactJS component. This is similar to giving an instruction to a friend, "Hey, when you go to the park, wear the green hat." The green hat is the special instruction or 'prop' you are passing to your friend.

In React, we use a similar concept called 'props' to pass data from one component to another. We can use props to pass a class name to a React component like so:

function HelloWorld(props) {
  return <h1 className={props.greetingStyle}>Hello, world!</h1>;
}

ReactDOM.render(<HelloWorld greetingStyle="greeting" />, document.getElementById('root'));

In this example, greetingStyle is a prop that we pass to the HelloWorld component. The value of greetingStyle ("greeting") becomes the class name of the h1 element.

Understanding the Code

If we were to translate this into a real-world scenario, it would be like telling a chef (our component) to cook a specific dish (our element) in a certain style (our class name). The chef will cook the dish according to the style we specified.

The same thing happens in our code. The HelloWorld component receives an instruction (the greetingStyle prop) and applies it to the h1 element by setting its class name to the value we passed ("greeting").

Using Class Names with Conditional Rendering

Sometimes, we might want to apply different class names based on certain conditions. This is similar to choosing what to wear based on the weather. If it's sunny, you might wear a hat. If it's raining, you'll probably pick a raincoat.

We can do the same in our React components using conditional rendering. Here's an example:

function HelloWorld(props) {
  const className = props.isHappy ? "happy-greeting" : "sad-greeting";
  return <h1 className={className}>Hello, world!</h1>;
}

ReactDOM.render(<HelloWorld isHappy={true} />, document.getElementById('root'));

In this example, if isHappy is true, the class name will be "happy-greeting". If isHappy is false, the class name will be "sad-greeting".

Conclusion

Just like how a chameleon changes its color based on its surroundings, our React components too change their appearance based on the class names we pass. It's a simple yet powerful way to create dynamic and responsive user interfaces. Learning how to effectively pass and use class names in ReactJS is akin to learning how to control the chameleon's color palette; it allows you to paint your web pages in any way you desire.

Remember, practice makes perfect. So, keep experimenting with different class names and props until you become a master of controlling your web components' styles. And remember, just as every chef adds their unique touch to their dishes, each developer has a unique way of coding. So, don't be afraid to add your own creative flair to your React components. Happy coding!