React Conditional Judgement
In React, you can dynamically render components or elements using JavaScript conditional statements.
The following are several common ways to handle conditional rendering in React:
1. Using if statements
Use if statements in the render method or in the return value of a function component to decide what to render.
Example
import
React from
'react'
;
import
ReactDOM from
'react-dom/client'
;
class
MyComponent
extends
React.
Component
{
render
(
)
{
const
isLoggedIn
=
this
.
props
.
isLoggedIn
;
let content
;
if
(
isLoggedIn
)
{
content
=
<
h1
Welcome back
!
h1
;
}
else
{
content
=
<
h1
Please sign up.
h1
;
}
return
(
<
div
{
content
}
div
);
}
}
const
root
=
ReactDOM.
createRoot
(
document.
getElementById
(
'root'
)
)
;
root.
render
(
<
MyComponent isLoggedIn
=
{
true
}
/>
)
;
2. Using the ternary operator
In JSX, you can use the ternary operator for concise conditional rendering.
Example
import
React from
'react'
;
import
ReactDOM from
'react-dom/client'
;
const
MyComponent
=
(
props
)
=>
{
const
isLoggedIn
=
props.
isLoggedIn
;
return
(
<
div
{
isLoggedIn
?
<
h1Welcome back
!
h1
:
<
h1
Please sign up.
h1
}
div
)
;
}
;
const
root
=
ReactDOM.
createRoot
(
document.
getElementById
(
'root'
)
)
;
root.
render
(
<
MyComponent isLoggedIn
=
{
true
}
/>
)
;
3. Using the logical AND (&&) operator
In JSX, you can use the logical AND operator for conditional rendering. If the condition is true, the element after it is rendered.
Example
import
React from
'react'
;
import
ReactDOM from
'react-dom/client'
;
const
MyComponent
=
(
props
)
=>
{
const
isLoggedIn
=
props.
isLoggedIn
;
return
(
<
div
{
isLoggedIn
&&
<
h1Welcome back
!
h1
}
{
!
isLoggedIn
&&
<
h1
Please sign up.
h1
}
div
)
;
}
;
const
root
=
ReactDOM.
createRoot
(
document.
getElementById
(
'root'
)
)
;
root.
render
(
<
MyComponent isLoggedIn
=
{
true
}
/>
)
;
4. Using switch statements
When you need to handle multiple conditions, you can use a switch statement in the render method.
Example
import
React from
'react'
;
import
ReactDOM from
'react-dom/client'
;
class
MyComponent
extends
React.
Component
{
render
(
)
{
const
userRole
=
this
.
props
.
userRole
;
let content
;
switch
(
userRole
)
{
case
'admin'
:
content
=
<
h1
Welcome
,
Admin!
h1
;
break
;
case
'user'
:
content
=
<
h1
Welcome
,
User!
h1
;
break
;
case
'guest'
:
content
=
<
h1
Welcome
,
Guest!
h1
;
break
;
default
:
content
=
<
h1
Who are you
?
h1
;
}
return
(
<
div
{
content
}
div
);
}
}
const
root
=
ReactDOM.
createRoot
(
document.
getElementById
(
'root'
)
)
;
root.
render
(
<
MyComponent userRole
=
"admin"
/>
)
;
Summary
ifStatement: suitable for use inrendermethods or in the return value of function components to decide what to render.- Ternary Operator: suitable for concise conditional rendering in JSX.
- Logical AND (
&&) operator: suitable for conditional rendering in JSX, rendering the element when the condition istrue. switchStatement: suitable for handling multiple conditions and rendering different content.