This module introduces you to some of the basic components in React, and what JSX is.
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
This Learning Path is aimed at all who wish to learn how to use the ReactJS framework.
We welcome all feedback and suggestions - please contact us at firstname.lastname@example.org to let us know what you think.
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.
Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.
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.