📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this chapter, we will learn how to render elements/components in React based on some condition. In other words, based on one or several conditions, a component decides which elements it will return.
In React, conditional rendering works the same way as the conditions work in JavaScript. We use JavaScript operators to create elements representing the current state, and then React Component update the UI to match them.
1. if-else Approach
function UserGreeting(props) {
return <h1>Welcome back!</h1>;
}
function GuestGreeting(props) {
return <h1>Please sign up.</h1>;
}
function Greeting(props) {
const isLoggedIn = props.isLoggedIn;
if (isLoggedIn) {
return <UserGreeting />;
}
return <GuestGreeting />;
}
ReactDOM.render(
// Try changing to isLoggedIn={true}:
<Greeting isLoggedIn={false} />,
document.getElementById('root')
);
2. Element Variables Approach
function LoginButton(props) {
return (
<button onClick={props.onClick}>
Login
</button>
);
}
function LogoutButton(props) {
return (
<button onClick={props.onClick}>
Logout
</button>
);
}
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;
if (isLoggedIn) {
button = <LogoutButton onClick={this.handleLogoutClick} />;
} else {
button = <LoginButton onClick={this.handleLoginClick} />;
}
return (
<div>
<Greeting isLoggedIn={isLoggedIn} />
{button}
</div>
);
}
}
ReactDOM.render(
<LoginControl />,
document.getElementById('root')
);
3. Inline If with Logical && Operator
import React, { Component } from 'react'
class UserGreeting extends Component {
constructor(props) {
super(props)
this.state = {
isLoggedIn: true
}
}
// #short-circuit-operator-approach
render() {
return this.state.isLoggedIn && <div>Welcome Ramesh</div>
}
}
export default UserGreeting
4. Ternary Operator Approach
import React, { Component } from 'react'
class UserGreeting extends Component {
constructor(props) {
super(props)
this.state = {
isLoggedIn: true
}
}
// ternary-operator-approach
render() {
return this.state.isLoggedIn ? (
<div>Welcome Ramesh</div>
) : (
<div>Welcome Guest</div>
)
}
}
export default UserGreeting
What's Next?
❮ Previous Chapter Next Chapter ❯
Comments
Post a Comment
Leave Comment