1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Style a component based on complex props with Styled Components

    Sara VieiraSara Vieira
    reactReact

    In this lesson, we will take a look at how to style your components very granularly based on their props.

    We will style a component based on a float value, string and his position in the array

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Sara Vieira: Let's look at this example where I have the list. I think it's 20 of the worst movies ranked on IMDB, which is a very important list to always have at hand which I do. It's very important. I also have a theme here, which has the background, the accents which is this color, foreground and then a red and a yellow that I want to apply based on the rating.

    We already passed the rating, so I get to movie.rating and I just parseFloat because it comes back from the API. By the API we mean JSON file as a string. I want to parse it as an int, in this case a float. We have the color which just returns the accent.

    One thing that we do is that in this case, instead of just returning something, we can create a function. Let's just return null yet again for now, so that our error goes away. As you can see, nothing new.

    The first thing is that I want to check if it's...I'm going to divide it from 2 to 2.3, 2.3 to 2.7 and then all the rest. I'm going to say if props.rating is smaller than 2.3, I want to return props.theme.red. What happens? Only these ones are red. OK. We're getting somewhere. Now, let me copy and paste this and we can say 2.7 and I can say yellow.

    Now, we can either put another if or we can just return. If I say return, if I can type return props.theme.accent and all the rest will be basically with this accent.

    Let's now do something to make this a bit cleaner. The only thing we need from here is the rating and the theme. I'm just going to get those two things, so that I don't have to do this with the props and it looks way cleaner.

    As you can see, you can basically do anything you wanted here. This is a normal function. You can even check for a name. Let me come here and add a background for example. I'm going to pass the movie title to the rating as well. I'm going to say title = movie.title and there we go. That's doing absolutely nothing.

    I'm going to check if it's "Saving Christmas" and if it is, I want the background to be black. I'm going to say background and I'm going to create another function. I'm going to say props, from there I want to say props.title. If it's equal to Saving Christmas, then give me back black. Otherwise, just give me null which is just going to say it's basically...Oh, sorry. It's basically transparent.

    As you can see, only this one actually has the background of black. Let me just see it. Yeah. Only this one has the background of black. You can actually do a whole bunch of different things. You can get very granular and select something very specific to have a certain style. Literally, the sky is your limit. You can even pass the array.

    For example...I'm just going to explain that. I have movies here and I can do something as in I pass the i, so I can remove this and I say length is movies.length. I say i = i. Then, I'm going to say i + 1. I'm going to say number = i + 1. Now let's check if it's the last one.

    We can easily do this even inside of here. I'm going to say if movies.length equals...If props.movies.length, sorry, props.movies.length === props.number. Let's just do this props.movies.length and then props.number, props.length. Now, you can see that only the last one has this.

    You can really do pretty much anything. It's just JavaScript, which is not just a just. Well, you can do pretty much everything because it's JavaScript and with JavaScript, the sky is the limit.

    This is how you can do more advanced things adjusting your styles using the props that come from your actual styled-components.