Complete React Tutorial (& Redux) #16 - Conditional Output
Key Takeaways
Demonstrates how to conditionally output parts of a template in React
Full Transcript
all right then gang so in this video I want to show you how to output something conditionally because sometimes we only want to show something on a web page to a user based on certain conditions for example only show something to a user if they are logged in or only show a user a discount if five items are more are added to the shopping basket so we'll look at how we can conditionally output content dependant on something in our ninja data that we receive as a prop right here so say for example I only want to output the ninja if their age is over 20 now that would be true for these two right here but since this is not over 20 we wouldn't want to output that so we're conditionally outputting something based on the data that we have right now there's a couple of different ways we can conditionally output something we could use an if statement to do that or we could use a ternary operator and I'm going to show you both different examples but we'll start with the if statement so right here when we're mapping through the original data ninjas and we're returning the new list we want that new list to contain only the ninjas whose age is over 20 so what we could do is we could just place an if statement above this return statement and we'll say if ninja age is over 20 then we're going to return this JSX right here so let's close that off like so and else what we're going to do is return null so what only returning some junior sex code if the ninja age is over 20 so let's see if that works let's go to the browser now and check this out and we can see that now we're only showing those two ninjas the one with the age of 20 is no longer showing so that's cool right now the other way to conditionally output something is by using the ternary operator in JavaScript so I'm going to show you that now so what I'll do is just comment this stuff out right here like so and I'll come on to that and this time I'll say Const ninja list is again going to be equal to ninjas map and we're going to take our ninja each time around pass that through into the arrow function but this time what we want to do is use the ternary operator instead to check whether we should output something or not now the way a ternary operator works in JavaScript is as follows we have first of all a condition which is going to evaluate to true or false where you place a question mark after that and then we have to return values here we have want return value which returns if it's true that condition then a colon and then the second return value which returns if that condition is false so this condition right here this is going to be the ninja age over 20 so this right here this statement is either going to evaluate to true or false if it if ru8 to true then we're going to return this stuff right here if it evaluates to false or return this thing right here now if it's false we just want to return null don't we because we don't want to output anything now we need to return this value inside here because remember but inside the map function and each time around it's expecting a return value and that return value exactly going to be this stuff in here this JSX we're about to put in here or null so let us now go inside here and add that JSX so it's just this stuff right here this is the JSX we want to return so let's copy it and paste it inside here and scoot that back like so we don't need these things so let's get rid of those like that and let's just neaten this up a little bit all right cool so now what we're doing is returning this JSX if this is true and this if it's false so let's see if this works so save that and view this in a browser and we can see this still works so there's two different methods they're using an if statement which is sometimes a bit simpler to grasp it first or using a ternary operator which I think sometimes leads to leaner coat now I just want to show you one more thing because we can't actually do all of this inside the template we turn right here if we want to I prefer to do all this stuff above but some people nest it directly in the template instead so what we could do is grab all of this stuff right here we're going to cut that and instead of outputting the Ninja list right here we're just going to output this stuff we've just got directly inside these little brackets right here because we're still outputting some dynamic code here so let's paste all that in and scoot it over to make it look a bit more presentable now we don't need this const here we don't need to define it anymore because we're not outputting if we're doing it directly so we'll just say ninjas dot map instead and we don't need this semicolon right here so what we're doing now is mapping through the ninjas and we're returning this thing right here which is either going to be this JSX or null so what only returning the JSX when the age is over 20 so now we're embedding this directly inside the returned template down here so let's save this and see if it works and yes it still does work but again I think this is sometimes a little bit confusing when you're looking at this and then you've got all the parts of the template going on as well a bit confusing I don't like that I like to do all of my kind of logic up here first of all put that in some kind of variable which we then add to the template down here and render it I think that is a lot simpler and we're kind of separating the logic from the actual template itself so it's up to you you can do both and you can choose whether to use the ternary operator or the if statement up here I think for the most part I'm going to be using this but occasionally you will see me use this as well and again for the most part I will be doing my logic up here but there we go that's how we conditionally output something to the template
Original Description
Hey gang, in this React tutorial I'll be showing you how to conditionally output parts of our template dependent on certain conditions within our data.To do this we'll use a combo of if statements and ternary operators.
----------------------------------------
🐱💻 Course Links:
+ VS Code editor - https://code.visualstudio.com/
+ GitHub repository (course files) - https://github.com/iamshaunjp/react-redux-complete-playlist/tree/lesson-16
+ React CDN - https://reactjs.org/docs/cdn-links.html
----------------------------------------
🤑 Donate @ https://www.paypal.me/thenetninja
🎓Find me on Udemy @ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related AI Lessons
⚡
⚡
⚡
⚡
Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI