image
Receiving and Using Props
Start course
Difficulty
Beginner
Duration
14m
Students
1362
Ratings
3.5/5
Description

This module looks at static versions in React, Props, and Mock Components to use when testing an application.

Learning Objectives 

The objectives of this module are to provide you with an understanding of: 

  • How to be able to build a static version of an application
  • How to use Props in components
  • How to be able to test Component snapshots and Components with props
  • How to be able to Mock components when testing

Intended Audience  

This Learning Path is aimed at all who wish to learn how to use the ReactJS framework.  

Prerequisites of the Course 

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

Now my trial component has been past propped how can I use them? For a component to receive its props, it has to know about the minutes function arguments. Props are passed as an object. So we have three options here. We can just pass the props object as a single argument to the arrow function like shown here. To use the specific props value inside the component, say header text, we need to prefix it with the props dot notation. 

Here's an example of what I mean. As you can see the application has rendered the value that has been passed from the parent component, where I've used props.headertext. The second and third options both use JavaScript object de-structuring. The size of the props object may determine which way you choose here. You can de structure the props object in the argument bypassing the arrow function and object, with all the proper names supplied to the component. So for our component, this would look like this. 

The other option, is to pass in the props object and then de structure in the component function body. In either case, the advantage of this is that we can simply use the prop name and the component code now, which makes it more readable. Now I can remove, the props dot notation, and achieve the same result. You'll notice that React has given warnings that we've got some unused values. This is common in React, and it helps you to minimize the amount of code that it gets bundled that isn't used. Once I've got these values in the component, I can use them as part of the component logic. These props can now be used as expressions in the return to display data, including calling the function. I can also use values as part of the component logic. 

Here we show a map function to calculate the next number when we add one to the current number in the sum array prop, and create an array of JSX expressions to display it, in the components return. You'll notice there's a warning here on the console. Each child in a list should have a unique key prop. This is because React needs to be able to identify each component it renders. And when we constructed the array, we didn't let React know which component was which. 

So to fix this, I would add a key property to, each of the JSX expressions. In this instance, I'll use the sequence number. As I do that, it removes the warning. We can also loop through the keys and values in the object prop, to create an array of JSX expressions to display these. The logic to create the array is just JavaScript that poses a JSX expression, to an array. So that's how we can receive props inside a component, de structure them so we can easily use their names, and use the values either in component logic, or as part of a JSX expression, to display on our screen.

Lectures

About the Author
Students
22644
Labs
6
Courses
29
Learning Paths
16

Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.

Previous roles have included being a Delivery Manager, Trainer, ICT teacher, and Head of Department. Ed continues to develop existing and new courses, primarily in web design using: PHP, JavaScript, HTML, CSS, SQL, and OOP (Java), Programming Foundations (Python), and DevOps (Git, CI/CD, etc). Ed describes himself as practically minded, a quick learner, and a problem solver who pays great attention to detail. 

Ed’s specialist area is training in Emerging Technologies, within Web Development. Ed mainly delivers courses in JavaScript covering vanilla JS, ES2015+, TypeScript, Angular, and React (the latter is authored by Ed) and has delivered on behalf of Google for PWAs. Ed has also developed a new suite of PHP courses and has extensive experience with HTML/CSS and MySQL. 

Ed is responsible for delivering QA’s Programming Foundations course using the Eclipse IDE. His skillset extends into the DevOps sphere, where he is able to deliver courses based around Agile/Scrum practices, version control, and CI/CD.

Covered Topics