How to load components conditionally in React JS

React JS is a popular library for making attractive and interactive user interfaces. Components in React JS refer to separate files for each building block of the application interface. They are a key feature of React JS that help to improve the usability of modules and create complex UI designs.

What is conditional loading?

Conditional loading is the capability to render the components on the interface according to the user's needs to improve interface interactivity. We load components conditionally in React js applications using two different ways.

  1. Conditional loading using useState

  2. Conditional loading using if/else

Concept

In this scenario, we are conditionally loading two components, Image.js and Description.js , to the Gallery made in App.js.

The initial interface of the application contains two buttons: View Image and View Description. Upon clicking any of the buttons, conditional loading occurs.

Basic interface with two buttons.
Basic interface with two buttons.

When View Image is clicked, the Image.js component is conditionally rendered in App.js. Hence, an image loads on the screen. In this case, if you again click on View Image, Image.js will unload and the image will disappear.

Image.js component is loaded.
Image.js component is loaded.

When View Description is clicked, the Description.js component is conditionally rendered in App.js. Hence, a description loads on the screen. In this case, if you again click on View Description, Description.js will unload and the description will disappear.

Description.js component is loaded.
Description.js component is loaded.

We can also load both components together to show the Image along with its description.

Image.js and Description.js components are loaded.
Image.js and Description.js components are loaded.

Let's conditionally load two components Image.js and Description.js by using each type of loading one by one.

Conditional loading using useState

We can use React js useState hook to set the visibility of the components to true or false when a state is changed i.e. on the click of a button in this case.

Example

const Image = () => {

    return(
        <div>
           <img src = 'https://media3.giphy.com/media/iicDrNGWxHmDrIni6j/giphy.gif'  alt = "img"/>
        </div>
    )
}

export default Image
Conditional loading using useState.

Code explanation

  • Line 1: Import useState from react.

  • Lines 2–3: Import components that are to be rendered.

  • Lines 7–8: Declare a React useState hook for each component that is to be rendered.

  • Line 14: Call a function when the View Image button is clicked that sets the image component to true.

  • Line 15: If the image component is set to true then load the Image.js component.

  • Line 16: Call a function when the View Description button is clicked that sets the description component to true.

  • Line 17: If the description component is set to true then load the Description.js component.

Conditional loading using if/else

We can set the visibility of the components to true or false in the parent component and pass it to the child component. In the child component, we can use if/else conditions to load the components conditionally.

Example

const Image = () => {

    return(
      <div>
      <div className="gallery">
        <center>
          <h1>Gallery</h1>
          <h3>This is an Image</h3>
          <img src = 'https://media3.giphy.com/media/iicDrNGWxHmDrIni6j/giphy.gif'  alt = "img"/>
        </center>
      </div>
      </div>
    )
}

export default Image
Conditional loading using if/else.

Code explanation

In index.js:

  • Line 10: Set the status of the isReadMode boolean to true or false and pass it to the App.js component as a prop.

In App.js:

  • Lines 2–3: Import components that are to be rendered.

  • Line 5: Pass a prop inside the function as a parameter to pass data from index.js to App.js.

  • Line 7: Declare a new variable and save the set mode of the application in it.

  • Lines 9–14: If the readMode is true load Description.js , else, if the isReadMode is false load Image.js.

Test your understanding

Q
      {showImg && <Image/>}

What does this mean?

A)

Load the Image component if showImg is set.

B)

Set showImg and then load Image component.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved