Passing className to components in React

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In React, we are familiar with the concepts of a className on components. It's the way React adds the class attribute to an element.
It looks like this:
<div className="bg-red-200"></div>
However, what happens if we want to make our custom components have the option to allow dynamic classNames?
To give you an example, let's say we have a CustomComponent that can take a className from wherever it's imported.
<CustomComponent className='bg-red-200` />
If we ran this in React, we would get shown an error as the CustomComponent doesn't know what a className is.
So let's fix that.
In all honesty, I probably made it sounds like it was going to be very complicated, right?
Well, fear not, it's not that scary, as we can pass the className as a prop!
So in your child component, we would structure its receiving end like this.
export default Child = ({ className }) => {
return (
<div className={className}>
<h2>I'm the child component</h2>
</div>
);
};
And now, we can use this component like this.
<Child className='bg-red' />
And that's it. Our component will now render this classname on its main div.
I also wanted to take a second to look at what happens if your child component always has certain classes and you want to add extra classes.
In that case, the props stay the same. However, we can dynamically render them like this.
<div className={`existing-class ${className}`}>
Our component will always render existing-class as a class but add whatever we set on it.
So if we use it like this:
<Child className='bg-red' />
Our result will be a div like this.
<div class="existing-class bg-red"></div>
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter