image
Conditional Rendering
Start course
Difficulty
Beginner
Duration
13m
Students
2271
Ratings
4.2/5
Description

This module introduces you to some of the basic components in React, and what JSX is. 

Learning Objectives 

The objectives of this module are to provide you with an 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

Sometimes we only want to display some markup when a particular condition is met or not met. Conditional rendering in React can be achieved in a few different ways. We can use an if else if else statement. Having each code block return JSX to be rendered when its condition is met. You can see here that the renderMe condition is true, and therefore react is rendering the JSX expression in the first block. C

hanging the value of renderMe to false gives access to the second condition, dontrenderMe is false, so the second JSX statement is rendered. Changing the dontrenderMe to true provides access to the final else block. Of course, a traditional switch statement could also be used here. We can also use Inline Conditional Rendering. Remember the JSX must evaluate as an expression. 

So using a traditional if statement is not possible. What we can do is use the ternary expression. Using the same two Boolean values as before, we can use ternary expressions to mimic the same behavior. In the first instance, we use a simple statement to display something if renderMe is true or not. Changing renderMe to false displays the second paragraph shown. 

We could then nest another ternary statement inside this to deal with the dontrenderMe condition. As you can see we quickly get into a nest of ternary statements, which is difficult to read and even harder to follow. Even though its functionality is the same. Changing the values of renderMe and dontrenderMe respectively to false and true exhibits the behavior we had before. The third way to do Conditional Rendering is by using an Inline Logical Expression. 

Here we use a logical and expression which evaluates condition and if it's true, it renders the JSX that follows. So for our situation, we can conditionally render each of the three JSX statements in our return. If it's logical condition is true. The first is displayed, because renderMe is true. The second will display if renderMe and dontrenderMe are those false. The third displays if renderMe is false, and dontrenderMe is true. We get the same functionality as we had in the previous two examples. I suppose it's for you to decide which way is most appropriate for your situation.

About the Author
Students
22498
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