Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

VueJS - Conditional Rendering

Using Conditional Rendering in VueJS

Conditional rendering in VueJS allows you to dynamically show or hide elements based on certain conditions. This guide covers the basics of using conditional rendering in VueJS, including the v-if, v-else-if, v-else, and v-show directives.

Key Points:

  • Conditional rendering allows you to control the visibility of elements in the DOM based on data conditions.
  • v-if, v-else-if, and v-else are used for complex conditional rendering.
  • v-show toggles the visibility of an element via the CSS display property.

v-if Directive

Basic Usage

The v-if directive conditionally renders an element based on the truthiness of an expression:



This text is visible

v-else Directive

The v-else directive is used to indicate an "else" block for v-if:



This text is visible

This text is hidden

v-else-if Directive

The v-else-if directive can chain multiple conditional blocks:



Type A

Type B

Type C

Other type

v-show Directive

Basic Usage

The v-show directive toggles the visibility of an element via the CSS display property. Unlike v-if, the element is always rendered and remains in the DOM:



This text is visible

Key Differences Between v-if and v-show

Understanding the differences between v-if and v-show can help you choose the right directive for your use case:

  • v-if: Conditional rendering that removes or inserts the element in the DOM based on the condition. Use v-if when you need to render elements conditionally and the condition is unlikely to change frequently.
  • v-show: Toggles the visibility of the element using the CSS display property. Use v-show when you need to toggle the visibility of an element frequently.

Best Practices

Follow these best practices when using conditional rendering in VueJS:

  • Use v-if for Conditional Rendering: Use v-if for elements that should be conditionally rendered and not just toggled.
  • Use v-show for Frequent Toggles: Use v-show for elements that need to be shown or hidden frequently without removing them from the DOM.
  • Avoid Complex Conditions in Templates: Keep conditions simple and readable. Move complex logic to computed properties or methods.
  • Use Key Attribute: When using v-if with v-for, always provide a unique key attribute for better performance and maintainability.
  • Group Related Conditions: Group related conditional blocks together to improve readability and maintainability.

Example Application

Here is an example application that demonstrates the use of conditional rendering with v-if, v-else-if, v-else, and v-show:



Conditional Rendering Example

This text is visible

This text is hidden

Type: {{ type }}

Type A

Type B

Type C

Other type

v-show Example

This text is always rendered but conditionally visible

Summary

This guide provided an overview of using conditional rendering in VueJS, including the v-if, v-else-if, v-else, and v-show directives. By understanding and utilizing these features, you can create dynamic and responsive user interfaces with VueJS.