Skip to main content

Command Palette

Search for a command to run...

Day 9 of 100days of code

Hello and welcome to day 9 of my challenge! πŸ’ͺπŸ’ͺπŸ™‹β€β™€οΈπŸ™‹β€β™€οΈ

Updated
β€’2 min read
Day 9 of 100days of code

Starting a new project

First, we create four different components, import them into App.jsx and add some basic stylings, and now it looks like this πŸ‘‡πŸ‘‡. Different colors indicate the different components created.

Basically, the App.jsx looks like this, with the Logo as the first component, the Form as the second, PackingList as the third, and the Stats as the last. πŸ‘‡πŸ‘‡

Rendering some list items

Now, let's render some dummy lists we created to display for now before we add the form to enable us to do that. Here, I created an array of two objects called initialItems, and we want to use it in the PackingList component. We have to pass it as props so initialItemscan be accessible in the PackingList component. πŸ‘‡πŸ‘‡πŸ‘‡

In the PackingList component, we destructure the props and use the map method on the initialItems array.πŸ‘‡

Let's further simplify the packing list by creating a component for the list. Here, we send the item object as an item prop to the new Item component like soπŸ‘‡πŸ‘‡

Result

Add some styles

Now, remember in the initialItems array, each object has a packed property which is either true or false. Let's add some CSS class to add dynamic style if the packed value is true. To test this out, we change one of the packed properties to true in our array and now conditionally render a style. At line 17πŸ‘‡πŸ‘‡

Since the packed value is true for the second list, the style applies.

Building a form and handling submission.

When we build a form in React, we use the <form> HTML element. Now, let's build our form and add an input, a select element, and a button, and add some styles.

So now we want to add some kind of event listener that listens for a submit event. For this, we create a function called handleSubmit and inside this, disable the default behavior of page refresh in HTML. Now, add the onSubmit event listener to the form.πŸ‘‡πŸ‘‡

Part 2 : Controlled elements

Sure thing! We will discuss controlled elements in detail tomorrow. πŸ’ͺπŸ™‹β€β™€οΈπŸ™‹β€β™€οΈ

N

Very cool post ! Way to go πŸ’ͺ. By the way you don’t need the key in the li inside the item : you already added it when doing the .map() 😊

1
C

Thank you So much for this πŸ™ . You mean at the point where I created a new Item component yeah? I see it now πŸ«‚πŸ«‚πŸ”₯

N

chioma christopher Sure ! Yes you just need it here when you render the item inside a map

1
C

Thank you so much I just updated it. Correct now?. Ndeye Fatou Diop

N

chioma christopher yep ! The way you can check is that normally if you don’t have the keys set up properly React should show an error in the console like Β« Warning: Each child in a list should have a unique β€œkey” prop. Β». If you don’t see it you’re good to go ☺️

1
C

Thank you so much for this. Ndeye Fatou Diop