Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

React Conditional Rendering

React Conditional Rendering

In React, you can create different components to encapsulate the various behaviors you need. Then, based on changes in the application's state, you can render only a subset of them.

Conditional rendering in React is the same as in JavaScript; use the JavaScript if operator or the conditional operator to create elements representing the current state, and let React update the UI based on them.

First, let's look at two components:

function UserGreeting(props) {
  return 

欢迎回来!

; } function GuestGreeting(props) { return

请先注册。

; }

We will create a Greeting component that displays one of the two based on whether the user is logged in:

React example

function

Greeting

(

props

)

{

const

isLoggedIn

=

props

.

isLoggedIn

;

if

(

isLoggedIn

)

{

return

<
UserGreeting
/>;
}
return
<
GuestGreeting
/>;
}
const
root
=
ReactDOM

.

createRoot

(

document

.

getElementById

(

"

root

"

)

)

;

root

.

render

(

//

Try changing isLoggedIn={true}:

<
Greeting
isLoggedIn
=
{
false
}
/>
)

;

Try it »


Element variables

You can use variables to store elements. This helps you conditionally render part of a component while leaving the rest of the output unchanged.

In the following example, we will create a stateful component named LoginControl.

It renders based on the current state: or , and it will also render the 。

React example

class

LoginControl

extends

React

.

Component

{

constructor

(

props

)

{

super

(

props

)

;

this

.

handleLoginClick

=

this

.

handleLoginClick

.

bind

(

this

)

;

this

.

handleLogoutClick

=

this

.

handleLogoutClick

.

bind

(

this

)

;

this

.

state

=

{

isLoggedIn

:

false

}

;

}

handleLoginClick

(

)

{

this

.

setState

(

{

isLoggedIn

:

true

}

)

;

}

handleLogoutClick

(

)

{

this

.

setState

(

{

isLoggedIn

:

false

}

)

;

}

render

(

)

{

const

isLoggedIn

=

this

.

state

.

isLoggedIn

;

let

button

=

null

;

if

(

isLoggedIn

)

{

button

= <

LogoutButton

onClick

=

{

this

.

handleLogoutClick

}

/>;

}

else

{

button

= <

LoginButton

onClick

=

{

this

.

handleLoginClick

}

/>;

}

return

(

<
div
<
Greeting
isLoggedIn
=
{
isLoggedIn
}
/>
{
button
}
div
)

;

}

}

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

(

"

root

"

)

)

;

root

.

render

(

<
LoginControl
/>
)

;

Try it »


Logical AND operator &&

You can embed any expression in JSX by wrapping code in curly braces, including JavaScript's logical AND &&, which conveniently allows conditional rendering of an element.

React example

function

Mailbox

(

props

)

{

const

unreadMessages

=

props

.

unreadMessages

;

return

(

<
div
<
h1
Hello

!

h1

{

unreadMessages

.

length

0

&& <

h2

You have

{

unreadMessages

.

length

}

unread messages.

h2

}

div

)

;

}

const

messages

=

[

'

React

'

,

'

Re: React

'

,

'

Re:Re: React

'

]

;

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

(

"

root

"

)

)

;

root

.

render

(

<
Mailbox
unreadMessages
=
{
messages
}
/>
)

;

Try it »

In JavaScript, true && expression always returns expression, while false && expression always returns false。

Therefore, if the condition is true, the element to the right of && will be rendered; if it is false, React will ignore and skip it.


Ternary operator

Another way to do conditional rendering is to use JavaScript's conditional operator:

condition ? true : false。

In the following example, we use it to conditionally render a small piece of text.

render() {
  const isLoggedIn = this.state.isLoggedIn;
  return (
    
The user is {isLoggedIn ? 'currently' : 'not'} logged in.
); }

It can also be used in larger expressions, though less intuitively:

render() {
  const isLoggedIn = this.state.isLoggedIn;
  return (
    
{isLoggedIn ? ( ) : ( )}
); }

Preventing component rendering

In rare cases, you may want to hide a component even when it is rendered by other components. This can be achieved by having the render method return null instead of its rendering result.

In the following example, conditionally renders based on the value of the warn prop. If warn is false, the component will not render:

React example

function

WarningBanner

(

props

)

{

if

(

!

props

.

warn

)

{

return

null

;

}

return

(

<
div
className
=
"
warning
"

Warning!

div

)

;

}

class

Page

extends

React

.

Component

{

constructor

(

props

)

{

super

(

props

)

;

this

.

state

=

{

showWarning

:

true

}

this

.

handleToggleClick

=

this

.

handleToggleClick

.

bind

(

this

)

;

}

handleToggleClick

(

)

{

this

.

setState

(

prevState

=>

(

{

showWarning

: !

prevState

.

showWarning

}

)

)

;

}

render

(

)

{

return

(

<
div
<
WarningBanner
warn
=
{
this

.

state

.

showWarning

}

/> <

button

onClick

=

{

this

.

handleToggleClick

}

{

this

.

state

.

showWarning

?

'

Hide

'

:

'

Display

'

}

button

div

)

;

}

}

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

(

"

root

"

)

)

;

root

.

render

(

<
Page
/>
)

;

Try it »

A component's render method returning null does not affect the callbacks of that component's lifecycle methods. For example, componentWillUpdate and componentDidUpdate can still be called.