Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
64 lines (54 loc) · 1.96 KB

File metadata and controls

64 lines (54 loc) · 1.96 KB
Copy raw file
Download raw file
Edit and raw actions
id logical-and-operator
sidebar_label Logical && operator
title Logical && Operator
description Logical && operator | React Patterns, techniques, tips and tricks in development for Ract developer.
keywords
logical && operator
react logical && operator
reactpatterns
react patterns
reactjspatterns
reactjs patterns
react
reactjs
react techniques
react tips and tricks
version Logical && operator
image /img/reactpatterns-cover.png

It happens often that you want to render either an element or nothing. You could have a LoadingIndicator component that returns a loading text or nothing. You can do it in JSX with an if statement or ternary operation.

function LoadingIndicator({ isLoading }) {
  if (isLoading) {
    return (
      <div>
        <p>Loading...</p>
      </div>
    )
  } else {
    return null
  }
}

function LoadingIndicator({ isLoading }) {
  return (
    <div>
      { isLoading
          ? <p>Loading...</p>
          : null
      }
    </div>
  )
}

But there is an alternative way that omits the necessity to return null. The logical && operator helps you to make conditions that would return null more concise.

How does it work? In JavaScript a true && 'Hello World' always evaluates to Hello World and a false && 'Hello World' always evaluates to false.

const result = true && 'Hello World'
console.log(result)
// Hello World

const result = false && 'Hello World'
console.log(result)
// false

In React you can make use of that behaviour. If the condition is true, the expression after the logical && operator will be the output. If the condition is false, React ignores and skips the expression.

function LoadingIndicator({ isLoading }) {
  return (
    <div>
      { isLoading && <p>Loading...</p> }
    </div>
  )
}

That's your way to go when you want to return an element or nothing. It makes it even more concise than a ternary operation when you would return null for a condition.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.