How to render LISTS in React πŸ“ƒ

Bro Code
31 Oct 202326:40

Summary

TLDRThis comprehensive tutorial delves into rendering lists in ReactJS, guiding viewers through the complexities of list rendering step by step. It begins by creating a functional component to manage a list, illustrating how to convert an array of strings into list items using JavaScript's map method. The video then explores sorting and filtering arrays, emphasizing the importance of unique keys for list items. It transitions to rendering arrays of objects, showcasing how to display additional properties like calories alongside fruit names. The tutorial also covers making the list component reusable by passing props, and concludes with practical tips on conditional rendering and setting default props and prop types for robust component design.

Takeaways

  • πŸ˜€ The tutorial focuses on rendering lists in ReactJS, which can be complex but is broken down step by step.
  • πŸ“ A new functional component named 'List' is created to manage the list, demonstrating basic component setup in React.
  • 🍎 An array of fruits is used to illustrate rendering arrays, highlighting the conversion of array elements into list items.
  • πŸ”„ The importance of using the 'map' method to transform arrays into list elements is emphasized for dynamic list rendering.
  • πŸ”‘ React's requirement for a unique 'key' prop for list items is discussed, ensuring React can efficiently update the DOM.
  • πŸ” The tutorial covers sorting arrays with the 'sort' method, including custom sorting functions for alphanumeric order.
  • 🏷️ The use of 'filter' method is explained to demonstrate how to display items based on certain criteria, like calorie count.
  • πŸ”€ The process of making the 'List' component reusable by passing different lists as props is detailed, enhancing component flexibility.
  • 🎨 Basic CSS styling is applied to the list components to improve the visual presentation of the rendered lists.
  • ❗️ Conditional rendering is introduced to handle cases where lists might be empty, ensuring the UI adapts to data changes.
  • πŸ› οΈ The tutorial concludes with setting up default props and prop types for the 'List' component to ensure robustness and error checking.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is rendering lists in ReactJS, covering various methods and best practices for handling and displaying lists of data.

  • How does the tutorial start in terms of creating a list component?

    -The tutorial starts by creating a new function-based component named 'list' in a JSX file within the source folder.

  • What is the initial data structure used in the tutorial for the list?

    -Initially, an array of fruit names is used as the data structure for the list.

  • How is the array of fruits transformed into list items in the component?

    -The array of fruits is transformed into list items using the built-in JavaScript 'map' method, which returns a new array of list item elements.

  • Why is it necessary to use a 'key' prop when rendering lists in React?

    -It is necessary to use a 'key' prop to give each list item a unique identifier, which helps React efficiently update and re-render the list when items are added, removed, or reordered.

  • How can you sort the list of fruits in the tutorial?

    -The list of fruits can be sorted using the 'sort' method, which can be applied directly to the array. The tutorial demonstrates sorting both lexicographically by name and numerically by calories.

  • What is the purpose of converting the array of strings into an array of objects?

    -Converting the array of strings into an array of objects allows for more complex data structures, enabling the inclusion of additional properties such as 'calories' and makes it easier to manage and display the data in the list.

  • How does the tutorial handle empty lists or missing data?

    -The tutorial uses conditional rendering to check if the list has elements before rendering it. If the list is empty or data is missing, it returns null or a default value to ensure the component doesn't try to render undefined data.

  • What is the benefit of making the list component reusable?

    -Making the list component reusable allows it to accept different types of lists as props, increasing its flexibility and reducing code duplication across the application.

  • How does the tutorial enhance the list component with CSS styling?

    -The tutorial enhances the list component with CSS styling by adding class names and defining styles for list categories and items, including hover effects, to improve the visual presentation of the list.

  • Why is it recommended to set up prop types in React components?

    -Setting up prop types is recommended for validating the data types of props passed to a component. It helps catch errors and provides warnings during development, ensuring that components receive the expected data types and improving code reliability.

Outlines

00:00

πŸ“ Introduction to Rendering Lists in React

This segment introduces the topic of rendering lists in ReactJS, emphasizing its complexity. The tutorial begins by creating a new functional component named 'List' within a JSX file. The component is initialized with an array of fruit names, and the video demonstrates what happens when this array is returned within the component. It then shows how to import and use this component in the main 'App' component. The focus is on converting the array of strings into an array of list item elements using the JavaScript 'map' method. The segment concludes with a discussion on rendering unordered lists and the importance of correctly enclosing elements within JSX.

05:01

πŸ”‘ Adding Unique Keys and Sorting Lists

The second paragraph delves into the importance of assigning unique keys to list items in React for better performance and state management. It demonstrates how to use the 'key' prop with the fruit names to avoid warnings. The tutorial then transitions into sorting the array of objects by different properties, such as name and calories, using the 'sort' method with custom comparison functions. The video also covers how to display additional information, like calories, alongside each fruit item and how to style the list items using bold tags. The segment ends with a discussion on sorting lists in both ascending and descending orders.

10:01

πŸ‡ Filtering and Reusable List Components

This part of the tutorial focuses on filtering list items based on specific criteria, such as calorie count. It shows how to create arrays for low and high-calorie fruits using the 'filter' method and conditional rendering. The video then shifts to making the 'List' component reusable by passing different arrays as props, including a demonstration of passing an array of vegetables. The segment also covers how to handle empty arrays and the use of default props to ensure that components render even when data is missing. Lastly, it introduces the concept of PropTypes for type checking in React components.

15:03

🎨 Styling Lists with CSS

The fourth paragraph is dedicated to applying CSS styles to the list components. It describes how to add class names to the list items and category headings, and then details the CSS properties used to style these elements, including font size, color, text alignment, and hover effects. The video provides a step-by-step guide on styling the list category with a border, background color, and other visual enhancements. It also covers styling the list items to improve readability and visual appeal.

20:07

βœ… Conditional Rendering and PropTypes

The final paragraph discusses conditional rendering to handle cases where lists might be empty. It explains how to use the logical AND operator for concise code when rendering components based on the presence of elements in an array. The tutorial also addresses the importance of setting default props and PropTypes to ensure that components behave correctly even when data is incomplete or incorrect. The segment concludes with a walkthrough of setting up PropTypes for the 'List' component, including defining the expected data types and shapes for the props.

Mindmap

Keywords

πŸ’‘ReactJS

ReactJS, often simply referred to as React, is a popular open-source JavaScript library primarily used for building user interfaces, especially for single-page applications. It is maintained by Facebook and a community of individual developers and companies. In the video, ReactJS is the main framework discussed, with the tutorial focusing on rendering lists within React components, which is a fundamental aspect of managing dynamic content in web applications.

πŸ’‘Component

In React, a component is a fundamental building block used to create a reusable piece of HTML structure, often encapsulating both data and behavior. The video script describes the creation of a new functional component named 'List', which is used to render lists of data. Components can be class-based or function-based, and in this case, a function-based component is created and exported for use elsewhere in the application.

πŸ’‘Array

An array in JavaScript, and thus in React, is a global object that is used to store multiple values in a single variable. In the script, arrays are used to hold lists of fruits and vegetables, which are then rendered within the 'List' component. Arrays are manipulated using methods like 'map', 'filter', and 'sort' to transform and display the data in various ways within the React component.

πŸ’‘Map Method

The map method in JavaScript creates a new array with the results of calling a provided function on every element in the calling array. In the video, the map method is used to transform an array of fruit names into an array of list item elements, which are then rendered within the 'List' component. This is a common pattern in React for rendering collections of data.

πŸ’‘List Item Elements

List item elements, such as 'li' for unordered list items or 'ol' for ordered list items, are HTML elements used to represent items in a list. In the context of the video, these elements are dynamically created from an array of data using the 'map' method and are then returned by the 'List' component to display the data in a list format on the webpage.

πŸ’‘Props

Props in React are a way to pass data from a parent component to a child component. The video script describes how an array of fruits and a category string are passed as props to the 'List' component. Props allow for making components reusable and customizable, as they can receive different data to render different content.

πŸ’‘Key Prop

In React, each child in a list should have a unique 'key' prop. The key helps React identify which items have changed, are added, or are removed. In the video, the tutorial initially encounters a warning about the need for a unique key prop, which is then resolved by assigning a unique 'ID' to each object in the array. This is crucial for performance optimization and accurate rendering of lists in React.

πŸ’‘Sort Method

The sort method in JavaScript sorts the array in place and returns the sorted array. In the video, the sort method is used on arrays of objects to order the list items by properties like 'name' or 'calories'. The tutorial demonstrates how to write custom sorting functions to handle both ascending and descending orders, which is essential for presenting data in a user-friendly manner.

πŸ’‘Filter Method

The filter method in JavaScript creates a new array with all elements that pass the test implemented by the provided function. In the video, the filter method is used to create subsets of the original array, such as filtering out fruits with low or high calorie counts. This method is showcased as a way to dynamically alter the data presented in the 'List' component based on specific criteria.

πŸ’‘CSS Styling

CSS (Cascading Style Sheets) is used to style the appearance of web content. In the video, CSS is applied to the 'List' component to enhance the visual presentation of the list items and category headings. The tutorial covers how to add classes to elements and define styles in a CSS stylesheet, which is an important aspect of web development for creating a polished user interface.

πŸ’‘Prop Types

Prop types in React are used to validate the props passed to a component, ensuring that they match the expected data types. The video script includes a section on setting up prop types for the 'List' component, which helps catch errors and enforces best practices. This is demonstrated as a way to ensure that the data passed to the component is of the correct type, preventing potential bugs and improving code maintainability.

Highlights

Introduction to rendering lists in ReactJS with a step-by-step tutorial.

Creating a new functional component named 'List' in a JSX file.

Exporting the 'List' component for use in other parts of the application.

Using an array of fruits to demonstrate rendering lists in React.

Returning an array within a React component and the need for importing it.

Converting an array of strings into an array of list item elements using the map method.

Using arrow functions for cleaner and more concise code.

Creating an unordered list with JavaScript embedded within JSX.

Sorting an array of strings lexicographically using the sort method.

Transforming an array of strings into an array of objects with additional properties.

Adding a unique key prop to each list item as recommended by React.

Using a unique ID as a key for list items to avoid naming conflicts.

Displaying additional data, such as calories, alongside each list item.

Sorting the array of objects by properties like name or calories.

Filtering objects based on criteria, such as calories being greater than 100.

Making the 'List' component reusable by passing different lists as props.

Applying CSS styling to list components for better visual presentation.

Using conditional rendering to display lists only if they contain elements.

Setting up default props to handle missing or undefined properties.

Establishing prop types for better debugging and data type validation.

Conclusion and encouragement for further study and practice with rendering lists in React.

Transcripts

play00:00

hey everybody I have a huge video for

play00:02

you today today we're going to talk

play00:04

about rendering lists in reactjs this

play00:07

can be pretty complex we'll go through

play00:08

it step by step what we'll do in this

play00:10

tutorial is create a new component to

play00:12

hold our list let's go to our source

play00:14

folder create a new file I'll name this

play00:17

component list this will be a jsx file

play00:22

to finish creating this component this

play00:24

will be a function based

play00:26

component with the name of list then be

play00:30

sure to export it export default

play00:35

list we'll start with something very

play00:37

simple we'll create an array of fruit

play00:40

this will be const fruits equals think

play00:44

of some fruit for me I'll pick an

play00:47

apple

play00:49

orange

play00:52

banana coconut and a

play00:57

pineapple let me show you what would

play00:59

happen if we were to return our array

play01:02

within our list component we will

play01:04

return our array of fruits but we will

play01:08

need to import it going back to the app

play01:11

component we will import our list

play01:14

component import list from a

play01:20

location SL list.

play01:24

jsx we need a return statement to be

play01:26

sure you have one we will return a list

play01:34

component and here is our list let me

play01:36

zoom

play01:37

in it's all one big string Apple orange

play01:41

banana coconut pineapple heading back to

play01:43

our list component we need to convert

play01:45

our array of strings into an array of

play01:48

list item elements we can use the

play01:50

built-in map method to do that we'll

play01:53

create a new array of list items the map

play01:56

method is going to return a new array

play01:58

after running it through a function

play01:59

function const list items equals take

play02:04

our original array of fruits use the

play02:07

built-in map method then we'll either

play02:10

pass in a callback a function expression

play02:13

or an arrow function we'll use Arrow

play02:16

functions because I like Arrow functions

play02:18

for every fruit in fruits Arrow meaning

play02:22

do this we'll create a new list item

play02:28

element that has the text of our fruit

play02:32

the array item we're not going to return

play02:34

our fruits array we're going to return

play02:36

an unordered

play02:39

list we're going to insert some

play02:42

JavaScript with curly braces we'll

play02:44

include our list items it's an array of

play02:47

list items and here's our array whoops

play02:51

but I forgot to enclose fruit with curly

play02:56

braces my mistake they are much better

play02:59

better or if you prefer we can turn this

play03:02

unordered list into an ordered list with

play03:05

a pair of O

play03:07

tags

play03:09

o there they're all numbered that's how

play03:11

to render a simple array if you're

play03:13

working with an array of strings and

play03:15

you'd like to sort this array beforehand

play03:17

you can use the sort

play03:18

method fruits. sort the sort method will

play03:22

sort an array in place however this

play03:24

doesn't work with numbers because we're

play03:26

sorting

play03:28

lexicographically we would treat numbers

play03:29

and symbols as characters more on that

play03:32

later let's go over example two we'll

play03:35

convert our array of strings into an

play03:37

array of objects each object will have a

play03:40

name property and calories calories per

play03:44

serving so let's enclose all of our

play03:46

elements within a set of curly braces to

play03:48

make them

play03:55

objects I'm going to place each of these

play03:57

objects on a new line just so that I can

play03:59

read it more

play04:03

easily each of these objects will have a

play04:05

name property the name property will be

play04:08

set to the original string for our array

play04:11

the first object will have a name of

play04:13

Apple let's add calories too calories

play04:15

per

play04:17

serving I did a quick Google search of

play04:19

some of the calories per serving for

play04:21

these fruits I don't know how accurate

play04:23

these are but they seemed right our

play04:25

first object has a name of Apple

play04:27

calories is set to 95

play04:31

name orange

play04:34

calories

play04:37

45 name

play04:41

banana calories

play04:45

105 name

play04:51

coconut coconut

play04:54

calories

play04:57

159 name pineapp

play05:00

example

play05:02

calories

play05:05

37 all right we now have an array of

play05:08

objects there's a few changes we're

play05:10

going to make if I run this again our

play05:13

list is in rendering I need to display

play05:15

the name property of each fruit fruit.

play05:19

name

play05:20

property we have one issue behind the

play05:22

scenes I'm going to right click go to

play05:25

inspect then go to

play05:28

console

play05:33

warning each child in a list should have

play05:35

a unique key prop react wants us to

play05:38

assign a key to each list item each key

play05:41

should be unique in my array of objects

play05:44

each fruit has a unique name I could use

play05:47

that we will set the key attribute to

play05:50

equal include some

play05:52

JavaScript fruit. name that warning

play05:56

should go

play05:58

away

play06:01

which it did if there's a possibility

play06:04

that two objects can share the same name

play06:06

you'll want to use some sort of unique

play06:08

ID in a real world scenario if you're

play06:11

pulling data from a database each row of

play06:13

data is going to have some sort of

play06:15

unique ID so we're going to mimic that

play06:18

let's add a new property for an

play06:20

ID ID will be one for Apple these will

play06:24

be in ascending order orange will have

play06:27

an ID of two banana is

play06:30

three coconut is four pineapple is

play06:35

five we'll set the key to be each

play06:39

ID this would be much better than using

play06:42

the name you can have a naming conflict

play06:44

if two objects share the same name like

play06:47

if these were people you could have two

play06:48

people named Bob that warning should be

play06:53

gone which it is react would like a

play06:56

unique key for each item just so it can

play06:58

more easily keep track of items being

play07:00

inserted and removed along with each

play07:03

element I'm going to display the

play07:04

calories next to each

play07:06

element so we'll make a few changes I'm

play07:09

going to put these HTML elements on a

play07:11

new line for

play07:12

readability after the fruit's name I'm

play07:15

going to add a

play07:16

colon a non-breaking space character and

play07:20

Pand nbsp for space oh then not a

play07:25

semicolon forgot about

play07:27

that then we'll ins some

play07:30

JavaScript fruit. calories for the

play07:33

calories of each

play07:34

fruit I'll make it bold I'll enclose our

play07:38

calories with a pair of bold tags which

play07:40

is just

play07:47

B now we're going to sort the items in

play07:49

our list we'll do that before the map

play07:52

method I'm going to sort our array of

play07:55

objects by their name property we'll

play07:58

take fruits

play08:00

use the sort method the sort method will

play08:02

sort an array in place we'll write a

play08:05

custom sorting function we have two

play08:08

parameters A and B A for the first item

play08:11

B for the second then we iterate over

play08:14

the entire

play08:15

array we need an arrow meaning do

play08:18

something to lexicographically sort

play08:21

string properties within an array we can

play08:23

use the following method we'll take the

play08:25

name property of a use the built-in Loc

play08:29

local or local some people say compare

play08:33

method I misspelled

play08:35

that yeah I can't spell

play08:38

compare to b.n name that should sort our

play08:43

array of objects by their name property

play08:46

for reverse order let me turn this line

play08:48

into a comment this is

play08:54

alphabetical for reverse alphabetical

play08:58

order we just have to replace a with B

play09:02

and B with a there pineapple orange

play09:05

coconut banana apple with apple being at

play09:07

the

play09:08

bottom let's sort by

play09:11

calories fruits. sort this one's easier

play09:15

again we have our parameters a comma

play09:18

B Arrow the calories of a a.

play09:22

calories minus b.

play09:26

calories that one's easy we have

play09:28

pineapple at the top followed by orange

play09:30

apple banana

play09:31

coconut this is in numeric

play09:34

order for reverse numeric

play09:41

order reverse

play09:43

numeric or descending you could say the

play09:47

calories of B minus the calories of a

play09:50

now we have coconut at the top with

play09:52

pineapple at the bottom with the least

play09:53

amount of

play09:55

calories in this next section I'm going

play09:57

to demonstrate to you how we can filter

play09:59

objects by a certain criteria we'll

play10:01

filter anything that's greater than 100

play10:03

calories we'll create a new array of

play10:05

fruit that has low

play10:07

calories we'll create a new array const

play10:10

low cal

play10:13

fruit equals take our original array of

play10:16

fruits use the built-in filter

play10:21

method we'll have one parameter of fruit

play10:25

examine each fruit in our array of

play10:27

fruits and Arrow then a condition here's

play10:31

the

play10:32

criteria check the calories property of

play10:35

our

play10:37

fruit if it's less than 100 filter it

play10:41

and add it to a new array instead of

play10:44

displaying our array of fruits let's

play10:46

display our new array of low calorie

play10:48

fruit oh that should be plural low cal

play10:50

fruits when we create our array of list

play10:53

items replace fruits with low cal fruits

play10:57

and any instance of fruit with local

play11:00

fruit

play11:02

singular so we have three instances of

play11:04

fruit let's replace

play11:07

those let me clean this up a

play11:10

little feel free to pause the video if

play11:12

you need to write this

play11:14

down we have three fruits that are low

play11:16

calories the calories is less than 100

play11:20

let's find any high calorie

play11:24

fruits we can just copy this line of

play11:26

code paste it but change the condition

play11:30

examine the calories of each fruit

play11:32

filter it out if the calories are

play11:34

greater than or equal to

play11:36

100 the name of this array will be high

play11:39

Cal

play11:41

fruits replace low calow fruits with

play11:43

high Cal

play11:45

fruits replace low cal fruit with high

play11:49

Cal fruit

play11:51

singular and do that for the other

play11:53

instances as

play11:56

well there we are we have two fruits

play11:58

that are high in calories bananas and

play12:01

coconuts that is US using the filter

play12:03

method to filter out list items let's

play12:06

replace High Cal fruits with fruits high

play12:09

Cal fruit with fruit to display our

play12:11

original

play12:18

array for the next part of this lesson

play12:20

we're going to transform this list

play12:22

component so it's reusable with

play12:24

different lists currently the way that

play12:25

we set this up is that each list

play12:27

component that we create has its own

play12:29

list of fruit so we're going to make

play12:32

some

play12:32

changes let's cut our list of

play12:36

fruits going to the parent component of

play12:40

app we'll paste our list of fruits then

play12:43

pass it as props to our list

play12:49

component with our list component we

play12:52

will have a key of items equals insert

play12:57

some JavaScript our list of fruits let's

play13:01

also add a category

play13:03

key category equals for my category

play13:08

let's say fruits eventually we'll add an

play13:11

H3 heading we're now going to send all

play13:14

of this data to the list component but

play13:17

we have to set up props within the list

play13:20

function we have a parameter of

play13:24

props we'll access the items of props to

play13:27

get our fruit

play13:30

we'll create const item list equals not

play13:35

to be confused with list items list

play13:38

items is what we get after we map

play13:41

it item list equals props do items and

play13:46

remember that items is our fruits

play13:49

array replace fruits with item

play13:53

list replace fruit with

play13:56

item do that here here and

play14:00

here there's our list

play14:03

again if you're going to sort or use the

play14:05

filter method be sure it's item list not

play14:08

fruits because now list is a reusable

play14:11

component I'll add our category two

play14:14

that's stored within props const

play14:17

category equals props do

play14:23

category currently we're returning a

play14:25

single ordered list I'll also include an

play14:27

H3 element

play14:32

include some JavaScript we need curly

play14:34

braces add our

play14:36

category however with our return

play14:38

statement we can only return a single

play14:40

element or many elements that are

play14:42

wrapped within a fragment let's create a

play14:45

fragment that will enclose all of our

play14:55

markup there we

play14:57

are going back to our app component

play15:00

let's create a new list just to make

play15:02

sure that our list component is

play15:05

reusable let's copy our array of

play15:09

fruits paste

play15:11

it we'll create an array of

play15:18

vegetables the IDS will be 6 7 8 9

play15:23

10 the name of my first item will be

play15:27

potatoes calorie 110 per

play15:30

serving

play15:32

celery calories will be

play15:37

15 carrots calories

play15:42

25 corn calories

play15:47

63

play15:50

broccoli calories

play15:53

50 now we should be able to create a new

play15:55

list

play15:57

component within our return turn

play15:58

statement we're going to need a fragment

play16:06

again we'll return a second list item

play16:10

component the items will be vegetables

play16:14

the category will be a string of

play16:17

vegetables boom there we go here's our

play16:20

second list

play16:21

component so our list component is now

play16:24

reusable we can pass in many different

play16:26

types of lists to make this look better

play16:29

let's apply some CSS styling we'll have

play16:31

to set a class name

play16:34

first our H3 element will have a class

play16:37

name equal

play16:39

to list-

play16:43

category our ordered list will have a

play16:46

class name of list

play16:51

items all right going to our CSS

play16:54

stylesheet index.css let's work on our

play16:57

list category

play16:59

first use dot to select a class list

play17:03

category I'm just going to run through

play17:05

these real quick I'm going to make sure

play17:08

I'm zoomed in to 100 because I was

play17:10

zoomed in

play17:11

beforehand font size 2.5

play17:16

em font weight will be

play17:22

bold pick a

play17:24

color I'm going to stick with hsl values

play17:28

I'll set the lightness to be

play17:31

20% I'll add a little bit of margin on

play17:33

the

play17:36

bottom 10

play17:39

pixels text align

play17:46

Center I'll add a

play17:48

border three pixel

play17:52

solid I'll add a border radius to round

play17:55

the

play17:57

corners

play18:02

and a background

play18:05

color pick a color again I'll use

play18:11

hsl this one I pre-picked already

play18:15

185

play18:17

100%

play18:19

50% that's not a bad looking color pick

play18:21

whatever color you would like let's work

play18:24

on our list item

play18:25

elements we need to select the class of

play18:28

list

play18:29

items do list items select each list

play18:33

item within this

play18:36

class I'll increase the font size to be

play18:39

2

play18:42

em I'll remove the list style but you

play18:45

don't have

play18:46

to list style will be

play18:50

none pick a color again I'm using hsl

play18:57

values I'll set the lightness to be

play19:02

25% text align

play19:07

Center and margin will be

play19:11

zero all right now when I hover over one

play19:14

of these list items I'll apply a hover

play19:18

effect take our class list items take

play19:21

each list item access the hover sudo

play19:24

class I'll change the brightness the

play19:27

color will be something a little bit

play19:29

brighter I'll set the lightness to be

play19:32

45% and then the cursor will be a

play19:37

pointer yeah not bad that's how we can

play19:40

apply some CSS styling to list

play19:42

components all right heading back to our

play19:44

app component what if we have an empty

play19:47

list for example with our fruits I'm

play19:49

going to cut

play19:51

it we can use conditional rendering to

play19:54

render our list only if there's elements

play19:57

let's put put those back we're going to

play20:00

write some JavaScript we need some curly

play20:02

braces let's take our array of fruits

play20:06

access the length

play20:08

property is it greater than

play20:11

zero question mark if it is we'll return

play20:15

our list

play20:18

component colon if it's false we can

play20:21

return null let's do that with our

play20:24

vegetables

play20:25

too take our array of vegetables

play20:28

AIS the length property is a greater

play20:31

than zero tary operator if it is return

play20:35

our list

play20:38

component if not we can return

play20:40

null if one of these lists doesn't have

play20:43

any elements we won't render that

play20:51

component there is a short hand to the

play20:53

tary operator we don't need to write or

play20:56

else return null what we'll use instead

play20:59

of the trary operator is the and logical

play21:01

operator we can effectively Short

play21:04

Circuit the first

play21:05

expression then we don't need colon null

play21:09

it's a little less

play21:13

code our list component will always be

play21:16

true because it

play21:18

exists however the first expression

play21:20

might not be so if this condition is

play21:24

false we don't render this if it is true

play21:27

we will render this let's try that again

play21:31

let's remove these

play21:33

elements we don't display the fruits

play21:35

list

play21:36

component and we will not display the

play21:38

vegetable list

play21:40

component this is known as short

play21:43

circuiting one thing to consider what if

play21:46

our category is missing let's remove our

play21:49

category from

play21:50

fruits well I would like to add some

play21:52

sort of placeholder here or what if one

play21:55

of these arrays was

play21:57

missing well nothing displays not even

play22:00

vegetables we should add some default

play22:02

props in case one of these properties is

play22:04

missing so going back to our list

play22:07

component before we export it let's set

play22:10

up some default props take our component

play22:13

name list. default props equals within a

play22:19

set of curly braces let's set our

play22:22

category category

play22:25

property to be category as a placeholder

play22:31

if for some reason these components

play22:33

don't have a

play22:34

category we'll add a placeholder of

play22:36

category which at least looks better if

play22:39

we're missing an array that's a bigger

play22:41

problem let me go to inspect then

play22:48

console we're trying to map something

play22:51

that's undefined we have no array to

play22:53

work with so nothing is rendering so as

play22:56

a backup Within default props let's set

play23:00

our items to be an empty

play23:02

array items will be an empty

play23:07

array if one of these arrays was missing

play23:09

for some

play23:11

reason at least the category is

play23:13

displayed as well as the subsequent

play23:15

components at least something will

play23:18

display okay then lastly as good

play23:20

practice if we're accepting props we

play23:23

should set up prop types I'll walk you

play23:25

through it if you're just joining us

play23:27

with prop types if the incorrect data

play23:30

type is passed into props when we debug

play23:33

it'll give us a

play23:34

warning to use prop types we have to

play23:37

import it at the top of our list

play23:39

component we will

play23:41

import prop

play23:43

types

play23:45

from prop

play23:49

types take our component of

play23:52

list

play23:54

list. prop

play23:56

types

play23:59

equals set of curly braces for our

play24:03

category this will be a

play24:05

string category colon

play24:08

space prop types do

play24:13

string add a comma for another line okay

play24:16

this is where it's going to get tricky

play24:18

we have an array of

play24:21

objects we'll access the items

play24:24

property items colon

play24:26

space to types I'm going to move down a

play24:30

little

play24:33

bit dot we have an

play24:35

array

play24:37

array of we have an array of

play24:41

objects prop

play24:43

types do shape method we have to define

play24:47

the shape of each object each object is

play24:50

going to have its own data types we have

play24:53

a number string then

play24:55

number we're defining an object we need

play24:57

a set of curly

play24:58

braces ID

play25:01

colon prop types.

play25:05

number comma for another

play25:08

property I'll put the next one on a new

play25:10

line for

play25:14

readability we have a name property

play25:16

which will be a string prop types.

play25:23

string then

play25:26

calories

play25:28

prop types.

play25:31

number and that's

play25:34

it let's head back to our app

play25:37

component our prop types should raise a

play25:39

warning if some of our data is of the

play25:41

wrong data type for example let's say

play25:44

that calories is now the string Apple

play25:48

maybe somebody mistyped it

play25:50

twice you can see that right here it

play25:52

changed let's go to

play25:54

inspect

play25:56

console

play25:58

and here's our warning because we have

play25:59

prop type set up invalid prop items

play26:03

index zero calories of type

play26:05

string if we didn't have prop type setup

play26:08

we wouldn't receive that warning this

play26:10

may go unnoticed it is good practice if

play26:12

you're accepting props to also set up

play26:14

prop types it's a little more

play26:16

complicated if you have an array of

play26:18

objects but here are the steps all right

play26:20

everybody I know this was a massive

play26:22

topic thank you all for watching feel

play26:24

free to take some time to study and work

play26:26

with this before moving on to the next

play26:28

topic if you would like we did Cover a

play26:30

lot of material and well those are

play26:32

various ways in which we can render

play26:34

lists and

play26:38

react

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
ReactJSList RenderingFrontend DevelopmentJavaScript TutorialWeb DevelopmentComponent PropsArray HandlingObject MappingCSS StylingPropTypes