1. Home
  2. Training Library
  3. Programming
  4. Courses
  5. React - Components and JSX

Conditional Rendering

Developed with
QA

The course is part of this learning path

ReactJS: Zero to Hero
course-steps
14
certification
7
lab-steps
3
description
1
play-arrow
Start course
Overview
DifficultyBeginner
Duration13m
Students229
Ratings
4/5
starstarstarstarstar-border

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 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

Sometimes we only want to display some marker 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-elseif-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. Changing 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 that JSX must evaluate as an expression, so using a traditionalist 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 a 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 that behavior we had before. The third way to do Conditional Rendering is by using an Inline Logical Expression. Here we will use a logical under expression which valuates 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 its logical condition is true. The first is displayed because renderMe is true. The second will display if renderMe and dontRenderMe are both 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
Students2953
Labs8
Courses29
Learning paths6

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.

Covered Topics