Add Conditional Styles to React Native With Classnames

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Building React Native Apps for Mac course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Building React Native Apps for Mac, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Building React Native Apps for Mac

So far we have style or application quite nicely. And it's pretty straightforward because we haven't gone into more complex cases . But as your application grows, you will start facing some subtle cases. And if you don't handle them properly, then it might get too complex to think about your styling and your state and your components themselves. So we're just going to explore this a little bit and how we can solve this on the long run. So let's say, for example, here on my book container, I want to change the title color based on the name of the book. So we already explore one technique of doing this, which is I could create a variable outside of my component itself, right? And I can call this title color. And here I can do some check. Let's say, for example, if the title includes the string Lord, and I just declare my colors in here. But this is quite cumbersome to do every single time, right? Especially if your component is going to grow, you're going to go back and forth, back and forth and create a bunch of styles. Sometimes you might get a little bit lazy and you might try to do something like this instead of creating the variable outside of the component. I'm just going to put it in line. And this effectively works. But as your component grows, you might start doing a lot of this right here. You might add some some other condition. Let's say instead of red, I also want it to be. I don't know. I want the fonts to be bold, right? But you kind of see or you kind of notice already as you go piling this up, it 's going to get harder and harder to understand your styling. And if it's embedded within all the other logic, it gets even harder to read. So this is definitely not scalable. So we're going to do something against this. In order to solve this, we're going to use another package. Let's add the dependency to the class names package. So the class names package is just a small utility that allows you to declare your styles as an object. And at the end, it takes a Boolean value, right? So the mapping is from a string to a Boolean value. And then it concatenates all the values or all the keys that have a true value. So let us just try it here. And I'm going to import Cn from class names. And instead of doing this, what I'm going to do is I'm going to pass inside of the tail in function, the Cn function. This one takes first of all any number of arguments. So I'm going to pass the first part of my styles because those are static, those don't change. Then I'm going to pass an object. Inside of the object, I'm going to pass first this one, right? Because this is the first part of my ternary operator. And because these two ternary operators are the same, they're both checking for the same condition. I'm going to add the bold font to it. And then I'm going to create a condition here. I'm going to place a condition in here that's going to evaluate once the component runs. I'm going to do something similar for my other styling. But this time I'm going to negate the property or the condition. And here, yeah, that's fine. I'm just going to change the text. And if I save it, you can see it now works. So let's change this. How do we test this? Let's change our book. Let's go for the name of the wind. And there you go. So you can see this is a lot more readable, right? You can quickly check for the condition at the end. And you just know that this is going to be appended. We can shorter this even more because at the end of the day, you're going to be combining both of them quite often, right? So there's no need for you to import each of them separately and applying them every single time. So I'm going to go to my tailwind file and I'm going to create or I'm going to export another function. I'm going to call this CW. This is the combination of class names plus tailwind. And this one's going to take undefined number of arguments. I don't care too much about the types right now. If you want, you can try to extract the types, but it doesn't matter. And I'm just going to do the same, right? I'm going to apply CN to TW and I'm just going to pass all my arguments into it . I also need to import my CN function and that's it. So now instead of importing TW from my package, I can just import CW and I can directly apply it here, which means I don't need this wrapping and this wrapping and it still works. All right. You could go even further, right? And do it on the component level. So create wrappers for all your base components where the style cannot only take an object, but whenever it takes or whenever it takes the final object, it directly runs it through the CW function. But that's an extra, we don't need to do it. That's good enough for our purposes. You