Day 9 - Passing Props In Styled Components
Tags: react, beginners
Published: 2020-09-24 15:39:07

As a public school English teacher in my previous life, I amassed PowerPoint files of games filled with vocabulary games for my elementary students to review. I worked for South Korea's foreign English program which gave me some advantages. The curriculum was set by the government and my lesson plans are based off of textbooks that were provided to us.

One of main problems was that these textbooks changed versions every year and so did the content. What I created for one year did not work for the following year, I had to update them. 

I build this game in two days to help alleviate the repetition of recreating materials. 

![game preview](https://media.giphy.com/media/NHvmlGHKnQJDllGhcX/giphy.gif)

This is one of many games I have to built!

## What I Learned

Making this particular game gave me solid review in Javascript and React. To get the alternating patterns like you see above, I created a function to pass a random color prop to my word item component. This was a more advanced way of utilizing the styled-components in React than I have previously done!

## How This Will Help Me

Today I want to help you in showing how I went about generating random colors for my application!

I have a parent app that handles the randomization of colors and then the list of colors gets mapped as a prop to the item component and passed down to the styled-components component.

### Set Up

First I set up my list of colors which is a constant variable meaning this won't change. 

![color list](https://dev-to-uploads.s3.amazonaws.com/i/zmd1m9hbomg402jokh6a.png)


I utilized useState and useEffect hooks to generate a randomized list. Here I declared states for an empty random list and update as true.  Update won't be true for long because when useEffect runs after the component mounts, it will change update as false. We will revisit this.

![parent](https://dev-to-uploads.s3.amazonaws.com/i/clvo6t7o8x4kgdzolstw.png)

### Random Function

Now I want to write my random function that takes the list and loops through, randomly taking a color that doesn't repeat to create a new list.

![rand funct](https://dev-to-uploads.s3.amazonaws.com/i/v763dyda7dlexryy659d.png)

### useEffect For Changes
Going back to useEffect, I write a condition that if update is true, make a randomized list using my randomize function, then set the update state as false. If you keep update's value as true, your app will crash because of too many re-renders!

![use effect](https://dev-to-uploads.s3.amazonaws.com/i/xpf80xhlczzyt3q9y6ns.png)


Next I map item components to the random color list.

![list map](https://dev-to-uploads.s3.amazonaws.com/i/3dsu35mu7cp3qa6wgvm7.png)

My return statement quickly sets up the item components with a button that 'calls' the useEffect hook to update colors in state. The useEffect hook will generate a new list and set update as false again.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ky6okt1u2q5mdnyecjhi.png)


### The Child Component

The child component takes one prop which is color and it returns a styled-component component that I named ColorItem. What you see is actually two components and the color prop is being passed twice.

![child component](https://dev-to-uploads.s3.amazonaws.com/i/6ww5hi1xk3a36qq4m45e.png)

To set up this component I also pass color into the ColorItem component and write a conditional statement in the properties.

![styled component](https://dev-to-uploads.s3.amazonaws.com/i/ggfo5hdsrt8z8s10cu25.png)

So then we have the following:

![giphy random](https://media.giphy.com/media/NBK63Etqqifvlgkybl/giphy.gif)

You can check out the final code [here](https://gist.github.com/josno/b61b79ba01461c381ac93f41a6550570).


There is a whole lot of ways to optimize and improve on this. For example, what if I want to set colors for an undetermined number of items?

Onto the next challenge!