vuejs-render-condicional

Conditional Rendering in Vue.js

  • 4 min

In Vue.js, conditional rendering allows us to show or hide elements based on a condition that will occur in our JavaScript code.

For example, you can make a message appear only if the user is logged in, or a button only show if there are items in the shopping cart.

Vue.js evaluates the condition and automatically decides whether to show or hide the element.

In Vue, we have two ways to do conditional rendering: v-if and v-show. Let’s look at each of them 👇.

The v-if Directive

The most common way to conditionally show or hide an element is the v-if directive, which allows us to render an element based on a boolean expression.

  • If the expression is true, the element will be rendered
  • If it is false, the element will not be rendered

For example,

<div v-if="showMessage">
  <p>This message will be shown if showMessage is true</p>
</div>
Copied!

Let’s see it with a complete example

<template>
  <div>
    <p v-if="showMessage">Hello, LuisLlamas.es!</p>
    <button @click="toggleMessage">Toggle Message</button>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const showMessage = ref(true);

function toggleMessage() {
  showMessage.value = !showMessage.value;
}
</script>
Copied!
  • The message is shown if showMessage is true.
  • When clicking the button, the toggleMessage function changes the value of showMessage, toggling the message’s visibility.

The v-else and v-else-if Directives

Additionally, we can also use the v-else directive to show an alternative element in case the expression is false.

<div v-if="showMessage">
  <p>This message will be shown if showMessage is true</p>
</div>
<div v-else>
  <p>This message will be shown if showMessage is false</p>
</div>
Copied!

We even have a v-else-if directive if we need to handle multiple conditions. Let’s see it with an example

<template>
  <div>
    <p v-if="score >= 90">Excellent</p>
    <p v-else-if="score >= 70">Good</p>
    <p v-else-if="score >= 50">Pass</p>
    <p v-else">Failed</p>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const score = ref(85);
</script>
Copied!

In this example, a different message is displayed depending on the value of score.

Conditional Rendering with v-show

The v-show directive is similar to v-if, but instead of removing the element from the DOM, it simply changes its CSS display property to none to hide it visually.

This means that the element still exists in the DOM, but is not displayed on screen. Otherwise, its use is basically identical.

<div v-show="showMessage">
  <p>This message will be shown if showMessage is true</p>
</div>
Copied!

In this case,

  • The <div> element and its content will be displayed if the variable showMessage is true.
  • If the variable is false, the element will be visually hidden, but will still exist in the DOM.

v-show does not have an equivalent to v-else or v-else-if

Comparison between v-if and v-show

At first glance, v-show and v-if may seem very similar, but they have different uses. The difference lies in how they manage element visibility.

Characteristicv-showv-if
VisibilityChanges the CSS style display to noneRemoves or adds the element from/to the DOM
InitializationThe element is always present in the DOMThe element is only created when the condition is true
ReactivityUsually fasterCan be slower
Ideal Use CasesShowing/hiding elements frequentlyShowing/hiding elements under more specific conditions
  • v-show is more efficient when you need to toggle an element’s visibility frequently, as it only changes its CSS display property (without needing to add or remove the element from the DOM)

  • v-if, on the other hand, is more suitable when the element needs to be shown or hidden only under certain conditions.

For example, if you want to render a person’s Avatar that has an image, but a person doesn’t have an image and you don’t want it, you could use v-if. Because the component “doesn’t want / doesn’t need” the image.

But if you wanted to show an indicator when something happens in your application, we would use v-show. Because the component “does want” the indicator, but has it turned off.

Copied!