101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    React javascript framework

    Receive aemail containing the next unit.
    • Introduction to React Javascript
      • 1.1Understanding JavaScript: Basics and Fundamentals
      • 1.2Introduction to React Javascript
      • 1.3React Components and Props
      • 1.4Basic React Layouts
    • In-Depth into React
      • 2.1React State and Lifecycle
      • 2.2Handling Events in React
      • 2.3Conditional Rendering
      • 2.4Lists and Keys in React
    • Advanced React Concepts
      • 3.1Forms in React
      • 3.2Lifting State Up
      • 3.3Composition vs Inheritance
      • 3.4Context API in React
    • Real-world React Applications
      • 4.1Integrating with other Libraries
      • 4.2Code-Splitting in React
      • 4.3Adding React in an Existing Application
      • 4.4Deploying React Applications

    In-Depth into React

    Understanding Conditional Rendering in React

    programming language construct that performs actions according to boolean conditions

    Programming language construct that performs actions according to boolean conditions.

    Conditional rendering in React works the same way conditions work in JavaScript. It uses JavaScript operators like if or the conditional operator to create elements representing the current state, and let React update and render the appropriate components when your data changes.

    What is Conditional Rendering?

    In simple terms, conditional rendering in React is a way to render different user interface (UI) markup if a condition is true or false. It's a technique where we render different elements or components based on a condition.

    Using Conditional Rendering

    There are several ways to use conditional rendering in React. Here are a few common methods:

    If-Else Rendering

    This is the most straightforward method. You can use if and else statements in your component’s render method to decide what to return.

    render() { if (this.state.isLoggedIn) { return <UserGreeting />; } else { return <GuestGreeting />; } }

    Element Variables

    You can use variables to store elements. This can help you conditionally render a part of the component while the rest of the output doesn’t change.

    render() { let button; if (this.state.isLoggedIn) { button = <LogoutButton />; } else { button = <LoginButton />; } return ( <div> <Greeting /> {button} </div> ); }

    Inline If with Logical && Operator

    You can embed any expressions in JSX by wrapping them in curly braces. This includes the JavaScript logical && operator.

    render() { return ( <div> <h1>Hello!</h1> {this.state.isLoggedIn && <LogoutButton /> } </div> ); }

    Inline If-Else with Conditional Operator

    Another method for embedding expressions in JSX is to use the JavaScript conditional operator (condition ? true : false).

    render() { return ( <div> <h1>Hello!</h1> {this.state.isLoggedIn ? ( <LogoutButton /> ) : ( <LoginButton /> )} </div> ); }

    Preventing Component from Rendering

    In some cases, you might want a component to hide itself even though it was rendered by another component. To do this return null instead of its render output.

    function WarningBanner(props) { if (!props.warn) { return null; } return ( <div className="warning"> Warning! </div> ); }

    In the example above, the <WarningBanner /> is rendered depending on the value of the prop called warn.

    By mastering conditional rendering, you can significantly increase the dynamism and interactivity of your React applications.

    Test me
    Practical exercise
    Further reading

    Howdy, any questions I can help with?

    Sign in to chat
    Next up: Lists and Keys in React