The course is part of this learning path
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 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
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 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.