#thedivisor#Now let’s say you want to wrap the completed item’s text into another HTML tag, like <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><del></code> to strike it out. You can add even more newlines and parentheses so that it’s easier to nest more JSX in each of the cases:#thedivisor#This style works well for simple conditions, but use it in moderation. If your components get messy with too much nested conditional markup, consider extracting child components to clean things up. In React, markup is a part of your code, so you can use tools like variables and functions to tidy up complex expressions.#thedivisor#Another common shortcut you’ll encounter is the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND#:~:text=The%20logical%20AND%20(%20%26%26%20)%20operator,it%20returns%20a%20Boolean%20value." target="_blank" rel="nofollow noopener noreferrer" class="inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal">JavaScript logical AND (<code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">&&</code>) operator.</a> Inside React components, it often comes up when you want to render some JSX when the condition is true, <strong class="font-bold">or render nothing otherwise.</strong> With <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">&&</code>, you could conditionally render the checkmark only if <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">isPacked</code> is <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">true</code>:#thedivisor#You can read this as <em>“if <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">isPacked</code>, then (<code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">&&</code>) render the checkmark, otherwise, render nothing”</em>.#thedivisor#Here it is in action:#thedivisor#A <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND" target="_blank" rel="nofollow noopener noreferrer" class="inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal">JavaScript && expression</a> returns the value of its right side (in our case, the checkmark) if the left side (our condition) is <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">true</code>. But if the condition is <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">false</code>, the whole expression becomes <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">false</code>. React considers <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">false</code> as a “hole” in the JSX tree, just like <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">null</code> or <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">undefined</code>, and doesn’t render anything in its place.#thedivisor#<strong class="font-bold">Don’t put numbers on the left side of <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">&&</code>.</strong>#thedivisor#To test the condition, JavaScript converts the left side to a boolean automatically. However, if the left side is <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">0</code>, then the whole expression gets that value (<code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">0</code>), and React will happily render <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">0</code> rather than nothing.#thedivisor#For example, a common mistake is to write code like <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">messageCount && <p>New messages</p></code>. It’s easy to assume that it renders nothing when <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">messageCount</code> is <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">0</code>, but it really renders the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">0</code> itself!#thedivisor#To fix it, make the left side a boolean: <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">messageCount > 0 && <p>New messages</p></code>.
#thedivisor#Now let’s say you want to wrap the completed item’s text in another HTML tag, like <del> to strike it out. You can add even more newlines and parentheses to make it easier to nest more JSX in each case:#thedivisor#This style works well for simple conditions, but use it carefully. If your components become messy with too much nested conditional markup, think about extracting child components to make it cleaner. In React, markup is part of your code, so you can use tools like variables and functions to simplify complex expressions.#thedivisor#Another common shortcut you’ll notice is the JavaScript logical AND (&&) operator. Inside React components, it often comes up when you want to render some JSX when the condition is true, or render nothing if it is not. With &&, you could conditionally render the checkmark only if isPacked is true:#thedivisor#You can read this as “if isPacked, then (&&) render the checkmark, otherwise, render nothing”.#thedivisor#Here it is in action:#thedivisor#A JavaScript && expression returns the value of its right side (in our case, the checkmark) if the left side (our condition) is true. But if the condition is false, the whole expression becomes false. React sees false as a “hole” in the JSX tree, just like null or undefined, and doesn’t render anything in its place.#thedivisor#Don’t put numbers on the left side of &&.#thedivisor#To test the condition, JavaScript automatically converts the left side to a boolean. But if the left side is 0, the whole expression gets that value (0), and React will render 0 instead of nothing.#thedivisor#For example, a common mistake is to write code like messageCount && <p>New messages</p>. It’s easy to think it renders nothing when messageCount is 0, but it actually renders the 0 itself!#thedivisor#To fix it, make the left side a boolean: messageCount > 0 && <p>New messages</p>.