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.
Conditional loading using
useStateConditional 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.
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.
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.
We can also load both components together to show the Image along with its description.
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 ImageCode explanation
Line 1: Import
useStatefrom react.Lines 2–3: Import components that are to be rendered.
Lines 7–8: Declare a React
useStatehook 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.jscomponent.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.jscomponent.
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
Code explanation
In index.js:
Line 10: Set the status of the
isReadModeboolean to true or false and pass it to theApp.jscomponent 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.jstoApp.js.Line 7: Declare a new variable and save the set mode of the application in it.
Lines 9–14: If the
readModeis true loadDescription.js, else, if theisReadModeis false loadImage.js.
Test your understanding
{showImg && <Image/>}
What does this mean?
Load the Image component if showImg is set.
Set showImg and then load Image component.
Free Resources