In Vue 3, what causes the dynamic class value to change along with the ref value when using a function with the ref property for binding?

I'm currently working on Vue code that looks like this.

<template>
  <h1 :class="{ disabled: checkClass() }">Hello</h1>
</template>

<script setup lang="ts">
import { ref } from "vue";

const a = ref(1);

function checkClass() {
  if (a.value === 2) {
    return true;
  }
  return false;
}

setTimeout(()=>{
   a.value = 2;
}, 3000)

setTimeout(()=>{
   a.value = 1;
}, 6000)

</script>

<style>
.disabled {
  background: red;
  color: blueviolet;
}
</style>

After changing a.value twice, I noticed that the function seems to be triggered again, which is causing confusion.

I am curious to understand why the background color of

<h1 :class="{ disabled: checkClass() }">Hello</h1>
changes twice.

Any insights would be appreciated. Thank you.

Answer №1

In Vue templates, bindings are essentially computed properties.

<template>
  <!-- your code -->
  <h1 :class="{ active: toggle() }">Welcome</h1>
  <!-- how it works under the hood -->
  <h1 :class="computed(() => ( {active: toggle()} ))">Welcome</h1>
</template>

This means they automatically update when their dependencies change.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with Angular 2 loading properly on Internet Explorer 11

We are currently running an Asp.net MVC 5 web application integrated with Angular 2. The application functions smoothly on Chrome, Firefox, and Edge browsers, but encounters loading issues on IE 11, displaying the error illustrated in the image below: ht ...

Utilizing Jquery to create a carousel with looping functionality for flowing text

I am currently facing an issue with a carousel that contains multiple images and text. In order to make the text responsive, I have implemented a script called FlowText. The script works perfectly on the initial carousel image (the active one), but as soon ...

Is there a way to ensure the code inside the success function of a promise object is executed before the code that comes after the function

My function, handleWidgetDrop, calls another function called insertBlankWidget which in turn executes a certain function upon the return of a promise object. The issue I am facing is that the code following the call to insertBlankWidget in handleWidgetDr ...

What should be triggered when clicking on the cancel button in Bootstrap's modal: `close()` or `dismiss()`?

Bootstrap's modal offers two methods for hiding the dialog: close(result) (Type: function) - Used to close a modal by providing a result. dismiss(reason) (Type: function) - Used to dismiss a modal and provide a reason. Should I use close when the u ...

Issues with using this.transitionTo in Ember.js

Exploring the integration of Ember.js with Phonegap, I have implemented the Confirm API. However, I am encountering an issue where executing this.transitionTo('index') from the event-driven function onConfirm is proving to be a challenge. Any th ...

Modifying HTML files in a Node.js environment with the fs module

Struggling with learning node.js and back-end development, I encountered a problem that I can't solve. Can anyone lend a hand? I've been working on changing my HTML page from h1.html to h2.html, but I keep encountering the following error: (Error ...

Increase or decrease the quantity of items by cloning with Jquery and dynamically changing the ID

Currently, I am working on a jQuery clone project where I need to dynamically add and delete rows. Despite searching extensively on Stack Overflow and Google, I only have a basic understanding of how jQuery clone works. Any suggestions would be greatly ap ...

The integration between Vue and Laravel for sending emails is experiencing technical glitches and is currently not functioning as expected

Hello, I'm facing an issue and need your assistance. Despite trying various solutions, nothing seems to work for me, and I can't figure out why. Here is the snippet of my Vue app.js: import './bootstrap'; import { createApp } from &ap ...

Retrieving the HTML ID attribute of an anchor link within a form and sending it to the backend server

I am currently working on a project that involves a form containing an anchor link tag with a dynamic ID. I am utilizing this anchor link tag to submit the form via Javascript. However, I am facing difficulty in fetching the ID of the same anchor link tag, ...

What is the most efficient way to transfer a string to a python program using ajax and retrieve a string in return?

I'm experiencing a problem with my ajax request where I am sending an object to a python program using JSON: $.ajax({ url: "http://localhost/cgi-bin/python.cgi", type: "POST", data: JSON.stringify(myobject), dataType: ...

Fill in a text box with a chosen value from a linked drop-down menu

My database contains two tables: 1 - tbl_category 2 - tbl_shelf_place I am working towards displaying the shelf_code in a textbox based on the selected category_name from a drop-down (book_category) with a value of category_id, instead of using another ...

Tips for optimizing performance by preloading external CSS and JavaScript files from a third-party source in a ReactJS project with webpack

Is there a method similar to using ProvidePlugin for preloading jQuery that can be used to preload third-party CSS and JS files without using import in the entry JS file? The reason I am interested in preloading these files is because I encountered an err ...

Create a horizontal scrolling div with a fixed position

Even though this topic has been discussed numerous times, I have not found any solutions that work for me. My webpage has a sidebar on the left with position:fixed in CSS, and I want it to scroll horizontally along with the rest of the content. For the he ...

Registering the service worker resulted in an error stating "Undefined is not a function"

When attempting to register a service worker using default React code, I discovered that some users were encountering a `TypeError: undefined is not a function` on the line `.then(registration => {` inside the registerValidSW function. Although it works ...

Using AngularJS in combination with a Flask backend to visualize the data being sent from the backend

I am currently developing a Single Page Application using Angular with a Python/Flask backend that connects to MongoDB. The challenge I'm facing is that, although I can retrieve data from the backend using a $http get request in Angular as an object ...

Having difficulty retrieving the user list from Firebase within Angular 6

I am facing an issue with retrieving the list of users from Firebase. Despite having values already set in the database, I am getting empty arrays. The strange thing is that I can successfully add users to the Firebase database and retrieve the list of us ...

What is the best way to halt a for loop when the API response is not successful?

What's the best way to halt a for loop if the api response is false? Currently, my code integrates with an api by making calls within a loop. I'd like to continue calling the api only if the response is true. Here is the current implementation: ...

What is the best way to configure the loading state of my spinner?

When a user clicks to navigate to the articles page, I want to display a spinner while waiting for the articles data to be fetched and displayed. There is a slight delay after the click, hence the need for the spinner. I have a custom spinner component ca ...

What is the best approach for managing caching effectively?

My SPA application is built using Websocket, Polymer, ES6, and HTML5. It is hosted on a Jetty 9 backend bundled as a runnable JAR with all resources inside. I want to implement a feature where upon deploying a new version of the JAR, I can send a message ...

"In Typescript, receiving the error message "Attempting to call an expression that is not callable" can be resolved

I am in the process of creating a function that matches React's useState signature: declare function useState<S>( initialState: S | (() => S), ): [S, React.Dispatch<React.SetStateAction<S>>]; Below is an excerpt from the functi ...