Have you tried React classnames?

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...

Regarding classes in React, we get a lot of power out of the box. However, we can enhance this power to make it even better.
We can leverage the classnames package, which has been in the React ecosystem since 2015.
If all you do is write static classes like this:
<div className='static-class'></div>
This article might not be for you. Not to disrespect you, but in that case, keep doing what you are doing.
React classnames rock when it comes to dynamic classes or combined classes.
A combined class, for instance, if we allow the className to be passed from a parent component like this.
<div className={`existing-class ${className}`}>
Or even classes that render conditionally.
<div className={`existing-class ${success && 'bg-green'}`}>
Let's see how this would like with React classnames, but before we dive into that, we have to install it first.
npm install classnames
I tend to import it differently, so I never get confused with the default className by doing this.
import classnames from 'classnames';
And now we can use it like this.
<div className={classnames('foo', 'bar')}></div>
Or, in the more advanced examples, we can do stuff like this:
<div className={classNames('existing-class', className)}>
<div className={classNames('existing-class', 'bg-green': success)}>
And this is where it shines. It's a great way to abstract, difficult class conditions to be more readable.
For the result of your output code, it doesn't matter which way you go. It will be the same. However, this is way easier and more consistent across your code base from a maintainability point of view.
If you are an avid React user and use conditional classes, I would like you to try classnames.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter