Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Beginners

    Think Like a Pythonista — Building a Book Sharing App

    LinkedIn

    Analyzing LinkedIn Automation’s Impact on Lead Generation

    JavaScript

    Visualizing Logs from a Dockerized Node Application Using the Elastic Stack

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, August 27
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / React / Conditional Rendering Methods in React
    JavaScript

    Conditional Rendering Methods in React

    Kingsley SilasBy Kingsley SilasJanuary 17, 2020Updated:May 26, 2024No Comments11 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Conditional Rendering Methods in React
    Conditional Rendering Methods in React
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Conditional Rendering Methods in React
    Conditional Rendering Methods in React

    While conditionals are common and useful when building web applications, it can be tricky trying to implement such in a React component. JSX makes it possible to reuse the conditionals you are already familiar with when working with JavaScript, as such, you need not learn a new one. In this tutorial, we’re going to be looking at some of the best approaches to handling conditionals when working with React. Most of the code we’ll try out will make use of a login and logout logic – the component will either render the view for logging in or logging out depending on a particular condition. This is similar to how your favorite social media platform works, you’ll only see the login page when you are logged out.

    When using a conditional in a component, the component has to determine what to render based on a defined condition. This is similar to how conditionals work in all programming languages.

    If/else

    This is the most popular way to handle conditionals in JavaScript and React. It is possible that it is the first that comes to mind when there’s a need to make use of conditionals. In this case, we will make use of the if/else statement to conditionally render a login or logout view. Let’s start with setting up the constructor part of our component

    class App extends React.Component {
      state = {
        username: "",
        input: "",
        isLoggedIn: false
      }
    }

    We will be making use of isLoggedIn to determine what gets rendered to the user. Such that when the value is true, (which means a user is logged in), we’ll display the option to logout. For us to do that we’ll make use of a method that gets called so that the state of the application can change.

    handleInput = (e) => {
        this.setState({ input: e.target.value })
      }
      
      handleLogin = () => {
        this.setState({
          isLoggedIn: true,
          username: this.state.input
        })
      }
      
      handleLogout = () => {
        this.setState({
          isLoggedIn: false,
          username: ""
        })
      }

    handleInput() will be called when the value of the input field changes. The input field will be used to collect the username of the person who wants to log in. For the purpose of this tutorial, we will not have any form of validation for the field. On hitting the submit button after the username has been entered, we make use of handleLogin() to set the username and change the value of isLoggedIn from false to true. handleLogout() does the opposite of that – it clears the username and set isLoggedIn to false.

    With that, the render() method will look like this.

    render() {
        const { username, isLoggedIn } = this.state
        if (isLoggedIn === false) {
          return (
            <div className="box">
              <form onSubmit={this.handleLogin}>
                <h1>Login Here!</h1>
                <div className="field">
                  <label
                    className="label"
                    htmlFor="username">
                    Username:
                  </label>
                  <div className="control">
                    <input
                      type="text"
                      name="username"
                      onChange={this.handleInput}
                      className="input"
                    />
                  </div>
                </div>
                <div className="field">
                  <button className="button is-light" type="submit">Submit</button>
                </div>
              </form>
            </div>
          )
        } else {
          return (
            <React.Fragment>
              <div className="box">
                <p>Hello {username}!</p>
                <p>Welcome onboard!</p>
                <div className="field">
                  <button
                    className="button"
                    onClick={this.handleLogout}
                  >
                    Logout
                  </button>
                </div>
              </div>
            </React.Fragment>
          )
        }
      }

    You can see that we make use of the if/else statement to determine which gets rendered. What gets rendered is dependent on the value of isLoggedIn. Since the code is not too much, we can easily have them all in a single render() method. Else, to make things cleaner, we’d have opted to have separate components for the logging in and out. Here is a demo pen you can check out to see how the above example works.

    Ternary Operator

    This is my favorite because it makes the code look cleaner and concise, compared to using an if/else statement. The ternary operator is wrapped in curly braces and begins with the condition, which is then followed by a question mark. After that, you have what you want to render when the condition is met, next is a colon and what should be rendered when the condition is not met.

    {
      user === "Kingsley" ? (
        <RenderKingsleyComponent />
      ) : (
        <RenderAnotherComponent />
      )
    }

    The condition is met when the value of user is Kingsley, this will cause <RenderKingsleyComponent /> to be evaluated, else <RenderAnotherComponent /> will be. Similar to what we have above, we can make use of the ternary operator in the place of the if/else statement like so.

    render() {
        const { username, isLoggedIn } = this.state;
        return (
          <React.Fragment>
            {!isLoggedIn ? (
              <div className="box">
                <form onSubmit={this.handleLogin}>
                  <h1>Login Here!</h1>
                  <div className="field">
                    <label className="label" htmlFor="username">
                      Username:
                    </label>
                    <div className="control">
                      <input
                        type="text"
                        name="username"
                        onChange={this.handleInput}
                        className="input"
                      />
                    </div>
                  </div>
                  <div className="field">
                    <button className="button is-light" type="submit">
                      Submit
                    </button>
                  </div>
                </form>
              </div>
            ) : (
              <div className="box">
                <p>Hello {username}!</p>
                <p>Welcome onboard!</p>
                <div className="field">
                  <button className="button" onClick={this.handleLogout}>
                    Logout
                  </button>
                </div>
              </div>
            )}
          </React.Fragment>
        );
      }

    The option to log in will be rendered when isLoggedIn is false, else what will be shown will be the username entered and a button to log out. You can check out this pen to see the example above.

    Read More:  Effective Strategies for Managing Project Risks Successfully

    Returning null

    You might be thinking, what about cases where you want to evaluate a condition and render one thing if the condition is met. For such a scenario, you can choose to return null when the condition is not met. In a typical example, let’s say you want to render a button when a value evaluates to true, you can make use of what we have in this pen.

    class App extends React.Component {
      state = {
        showButton: true
      };
    
      render() {
        const { showButton } = this.state;
        { if (showButton) {
          return (
            <button className="button is-light" type="submit">
            Click Here!
          </button>
          )
        } else {
          return null
        }}
      }
    }

    The component will only render the button when showButton is true, nothing gets rendered if the value of showButton is false. You can also make use of what is called the logical && (AND) operator.

    Logical && Operator

    The issue with the above example is that we are handling scenarios where the condition is false, even though we won’t be rendering anything to the user. Using the logical && operator, we can do away with that part. So the above example will look like this.

    class App extends React.Component {
      state = {
        showButton: true
      };
    
      render() {
        const { showButton } = this.state;
        return (
          <div>
            { showButton && (
              <button className="button is-light" type="submit">
                Click Here!
              </button>
            )}
          </div>
        );
      }
    }

    You can check Codepen for a demo. This looks cleaner. We can take it further by adding a button to toggle the value of showButton, such that the submit button gets shown or hidden, as I did in this pen.

    class App extends React.Component {
      state = {
        showButton: true
      };
    
      toggleButton = () => {
        this.setState({
          showButton: !this.state.showButton
        });
      };
    
      render() {
        const { showButton } = this.state;
        return (
          <React.Fragment>
            <div className="box">
              <button
                onClick={this.toggleButton}
                className="button is-dark">
                Toggle Login Button
              </button>
            </div>
            { showButton && (
              <div className="box">
                <button
                  className="button is-light"
                  type="submit">
                Login Here!
              </button>
              </div>
            )}
          </React.Fragment>
        );
      }
    }

    Switch Case

    Switch case statement comes in handy when you have to handy multiple conditions. In React, switch cases are popularly used when making with Redux, in this tutorial we will see how to make use of switch cases for conditional rendering. Using the login and logout examples we have been using, we will separate these parts into their own components. Here is a pen that shows what we are working on.

    The Login component will look like this

    class Login extends React.Component {
      state = {
        input: "",
        username: ""
      };
    
      handleInput = e => {
        this.setState({ input: e.target.value });
      };
    
      handleSubmit = () => {
        const { handleLogin } = this.props;
        handleLogin(this.state.input);
      };
    
      render() {
        return (
          <div className="box">
            <form onSubmit={this.handleSubmit}>
              <h1>Login Here!</h1>
              <div className="field">
                <label className="label" htmlFor="username">
                  Username:
                </label>
                <div className="control">
                  <input
                    type="text"
                    name="username"
                    onChange={this.handleInput}
                    className="input"
                  />
                </div>
              </div>
              <div className="field">
                <button className="button is-light" type="submit">
                  Submit
                </button>
              </div>
            </form>
          </div>
        );
      }
    }

    This component will collect the username and send it to the parent component using this.handleLogin(). The username will be used in the Logout component, the parent component passes it as props to the Logout component where it is displayed, with the option to also log out by clicking a button.

    const Logout = props => {
      return (
        <div className="box">
          <p>Hello {props.username}!</p>
          <p>Welcome onboard!</p>
          <div className="field">
            <button className="button" onClick={props.handleLogout}>
              Logout
            </button>
          </div>
        </div>
      );
    };

    Now we can have the App component to look like this.

    class App extends React.Component {
      state = {
        username: "",
        isLoggedIn: false
      };
    
      handleLogin = value => {
        this.setState({
          isLoggedIn: true,
          username: value
        });
      };
    
      handleLogout = () => {
        this.setState({
          isLoggedIn: false,
          username: ""
        });
      };
    
      render() {
        const { username, isLoggedIn } = this.state;
        switch (isLoggedIn) {
          case false:
            return <Login handleLogin={this.handleLogin} />;
          case true:
            return <Logout handleLogout={this.handleLogout} username={username} />;
          default:
            return null;
        }
      }
    }

    This is similar to how we used the if/else statement, in both cases we’re making use of isLoggedIn to determine what gets displayed.

    Read More:  Monthly Digest of the Most Popular JS Github Repositories

    Higher-Order Components

    Higher-Order components are useful for cases where you want to reuse a certain logic across multiple components. With that, it is possible to include a conditional rendering inside the higher-order component, such that the component that is returned is dependent on the result of the condition. As we have in this pen, we are going to create a high-order component that handles cases of either returning the Login or Logout component. While reusing the Login and Logout component of the previous example, the higher-order component will look like this.

    const withGuest = WrappedComponent => ({ isLoggedIn, ...props }) => {
      if (isLoggedIn) {
        return <WrappedComponent {...props} />;
      }
    
      return <Login handleLogin={props.handleLogin} />;
    };

    The component passed to the withGuest component will be returned if is isLoggedIn evaluates to true, else the Login component will be returned.

    const Main = withGuest(Logout);
    
    class App extends React.Component {
      state = {
        username: "",
        isLoggedIn: false
      };
    
      handleLogin = value => {
        this.setState({
          isLoggedIn: true,
          username: value
        });
      };
    
      handleLogout = () => {
        this.setState({
          isLoggedIn: false,
          username: ""
        });
      };
    
      render() {
        const { username, isLoggedIn } = this.state;
    
        return (
          <Main
            isLoggedIn={isLoggedIn}
            username={username}
            handleLogin={this.handleLogin}
            handleLogout={this.handleLogout}
          />
        );
      }
    }

    Here are creating a new component called Main and we pass the Logout component to the higher-order component. The Main component received the needed prop to determine the value of isLoggedIn, and also to log the user in or out.

    Enum

    We can make use of JavaScript to create a conditional rendering in our React application. First, you need to know that you create an enum like this;

    const Size = {
      SMALL: 1,
      MEDIUM: 2,
      LARGE: 3,
    };

    In this tutorial, we will have the function that returns a component based on the value passed to it – the value matches the key of the component, the component is then returned.

    const getUserComponent = value => {
      return {
        loggedOut: (
          <Login
            handleLogin={value.handleLogin}
          />
        ),
        loggedIn: (
          <Logout
            username={value.username}
            handleLogout={value.handleLogout}
          />
        )
      };
    };

    We can then make use of the above function in a component.

    const UserState = ({ status, ...props }) => {
      return (
        <React.Fragment>
          {getUserComponent(props)[status]}
        </React.Fragment>
      )
    };

    This component will be included in our App component, it receives props from the parent (App) component, which it then uses the call the getUserComponent() function. In return, we either get the Login or Logout component. Here is how the App component looks like.

    class App extends React.Component {
      state = {
        username: "",
        status: "loggedOut"
      };
    
      handleLogin = value => {
        this.setState({
          username: value,
          status: "loggedIn"
        });
      };
    
      handleLogout = () => {
        this.setState({
          username: "",
          status: "loggedOut"
        });
      };
    
      render() {
        const { username, status } = this.state;
    
        return (
          <UserState
            status={status}
            username={username}
            handleLogin={this.handleLogin}
            handleLogout={this.handleLogout}
          />
        );
      }
    }

    We reused the Login and Logout component of previous examples, you can check out this pen.

    Conclusion

    The conditional rendering method you use in your application should be dependent on the complexity of the condition. For example, you won’t want to have if/else statements littered all over your component. You can make use of the logical && condition when you only need to render one thing based on the result of the condition. I find myself using the ternary condition over the if/else statement.

    JavaScript js react
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Kingsley Silas

      Related Posts

      Streamlining Resource Allocation for Enhanced Project Success

      December 18, 2024

      Conducting Effective Post-Project Evaluations: A Guide

      December 16, 2024

      Strategies for Keeping Projects on Track and Meeting Deadlines

      December 10, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      JavaScript October 8, 2019

      Monthly Digest of the Most Popular and Trending JS GitHub Repos

      In the following blog post, we’ll cover the most popular GitHub JavaScript repositories as of October 2019.

      Essential Steps to Craft a Winning Startup Business Model

      November 28, 2024

      Deep Learning vs Machine Learning: Overview & Comparison

      September 12, 2019

      Web Workers: Concurrency In Java Script

      February 15, 2018

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Maximizing LinkedIn: A Strategic Approach to B2B Lead Generation

      LinkedIn November 24, 2024

      Why Using Node.js for Developing Web Applications Makes Perfect Sense

      JavaScript November 21, 2019

      Strategies for Keeping Projects on Track and Meeting Deadlines

      JavaScript December 10, 2024

      The Importance of Showing Off Your Soft Skills

      Job March 5, 2019

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Interview

      Essential Strategies for Success in Panel Interview Preparation

      Programming

      5. Уроки Node.js. Глобальные модули.

      Beginners

      Java Lambda Expressions

      Most Popular

      Уроки React. Урок 11. Pt.1.

      Programming

      Building a WordPress Website with React (Frontity)

      React

      The Path of the Self-Taught Programmer: Avoiding Common Problems

      Programming
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.