Menu

Conditional Rendering

Conditional Rendering

In React, conditional rendering is a way to control what a component displays based on a condition. If the condition is true, a specific UI element appears; if it’s false, it doesn’t show, or something else can appear instead. This helps make the interface dynamic and responsive, just as a program decides what to do next based on rules.

{inputValue && (
              <FontAwesomeIcon
                icon={faCircleXmark}
                className="clear-icon text-xl md:text-2xl absolute cursor-pointer"
                onClick={() => setInputValue("")}
              />
)}

Explanation:

This uses conditional rendering to display the clear icon only when inputValue contains text. If a value exists (true), the close icon appears; if it’s empty (false), nothing is shown. When the icon is clicked, setInputValue("") runs and clears the input field instantly.