1. Home
  2. Training Library
  3. Components and JSX

Creating Multiple JSX Elements From Data

Developed with
QA

The course is part of this learning path

ReactJS: Zero to Hero
course-steps
14
certification
5
lab-steps
3
play-arrow
Start course
Overview
DifficultyBeginner
Duration13m
Students64
Ratings
3/5
starstarstarstar-borderstar-border

Description

This module introduces you to some of the basics Components in React and what JSX is.  

Learning Objectives 

The objectives of this module are to provide you with and understanding of 

  • What components are 
  • What JSX is and why it is used in React JS 
  • How to create Functional and Class Components 
  • How to add multiple components 
  • How to inspect components in the Browser
  • How the in-browser tools work 

Intended Audience  

This learning path is aimed at all who wish to learn how to use the ReactJS framework.  

Prerequisites

It is essential you understand the face of contemporary web development to attend this course. We insist upon JavaScript experience, along with good HTML and CSS skills. 

Feedback 

We welcome all feedback and suggestions - please contact us at qa.elearningadmin@qa.com to let us know what you think. 

 

Transcript

- [Narrator] Three useful components are at the heart of pre-admin. Often, we need to create the same mark up with some dynamic content. For instance, we might have a bunch of items to display in a list or groups of data to display in table rows. As JSX can be returned from functions, we can create arrays of JSX expressions than can be used in the render part of a component. In the example shown, we have an array of strings that we would like to include in an ordered list. To do this, we could use a map function call within the return of our component, but this means our code isn't very clean. The result, however, is what we want which is each item in the array displayed in a list itemed element as you can see in the browser. Taking advantage of React's ability to render an array of JSX expressions and JavaScript Array Map function, which basically lets us take each element in an array and return an array of modified elements, we can move the map called to outside of the return of our component, making it cleaner. The map function is going to work on the array of list items, passing each element to the function as item and returning a JSX expression that interpolates the array item into an ordered list element, ultimately populating a new array called Item List. We then simply render the Item List array in our return. Notice that the console output is showing a warning about keys. This is because React needs to be able to track each DOM element tree it renders so that it can work out if the tree needs to be updated. In an array of JSX, there is no identifiable property of each DOM tree it renders. To combat this, react works best if you include a unique key attribute on each of the outer HDML links in the returned expression. Usually, data will come from a database and have an ID property that could be used but as a last resort, the index of the array element can be passed into the map function and used as the key value. This is what we have done here. However, using the array index can cause rendering issues if the array is modified and the key is the same as before. React assumes that the DOM element is the same as before and therefore may not update it. A better alternative would be to use a unique ID generator, like those in the MPN packages, UUID and short ID. You can see, when the outer HDML element of the JSX expression has a key attribute, the key warning is not shown on the console.

About the Author

Students1159
Labs8
Courses29
Learning paths5

An outstanding trainer in software development with more than 15 years experience as a Corporate and Apprentice Trainer, ICT Teacher and Head of Department, with a passion for technology and its uses. Continuing to develop existing and new skills and courses, primarily in web design using PHP, JavaScript, HTML, CSS and SQL but also OOP (Java), programming foundations (Using Python), DevOps (Git, CI/CD, etc) and Agile/Scrum. Practically minded, a quick learner and a problem solver with an attention to detail to ensure high quality outcomes.