The title attribute in Vue3 is updated through props, but computed properties remain unaffected

I incorporated an external library into my project using Vue3. The component I am utilizing is sourced from a third-party library [Edit: Upon realizing that the GitHub repository for this library is no longer being maintained, I have updated the code to reference the actual implementation in my node_modules.]

<template>
  <div class="socket" :class="className" :title="socket.name"></div>
</template>

<script>
import { defineComponent, computed } from "vue";
import { kebab } from "./utils";
export default defineComponent({
  props: ["type", "socket"],
  setup(props) {
    const className = computed(() => {
      return kebab([props.type, props.socket.name]);
    });
    return {
      className
    };
  }
});
</script>

The rendering of this component relies on a Socket object passed as a prop. While observable changes to the name property of the Socket successfully update the title, the corresponding CSS class fails to refresh. Despite attempting $forceRefresh() on the parent component, no updates are observed.

Update: After transferring the rendering logic to my personal repository, I now possess the capability to modify this component as required.

Upon inspection of the updated code snippet, it appears that the issue stems from the computed nature of the class. Is there a method to trigger a forced refresh in this scenario?

Notably, the class only refreshes when I manually reload the code during vue-cli-service serve, without necessitating a full page refresh.

To provide context, the | kebab filter function is defined as follows:

Vue.filter('kebab', (str) => {
    const replace = s =>  s.toLowerCase().replace(/ /g, '-');

    return Array.isArray(str) ?  str.map(replace) : replace(str);
});

Could filtered attributes behave distinctively in terms of reactivity? This notion seems unlikely.

Contemplating whether the issue pertains to reactivity, I pondered about setting the value with Vue.set. However, my understanding indicates that such actions are redundant in Vue3, especially considering the successful updating of the title content.

Answer №1

Vue considers computed properties to be reactive, but it is not recommended to mutate a prop object.

According to the documentation:

Warning

Please note that in JavaScript, objects and arrays are passed by reference. Therefore, if a prop is an array or object and you mutate the object or array directly within the child component, it will impact the parent state without Vue being able to warn you about it. It is generally advised to avoid mutating props, including objects and arrays, as this goes against one-way data flow and may lead to unexpected outcomes.

https://v3.vuejs.org/guide/component-props.html#one-way-data-flow

Although the documentation specifies not to mutate the prop "in the child", it is a good practice not to mutate properties at all. Instead, create a new object with the modified data.

In your scenario, the computed function will detect changes in the properties themselves, not their individual members, which is why it is not triggering updates.

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

SVG path with dynamic elements

Consider the following SVG: <svg xmlns="http://www.w3.org/2000/svg" width="237" height="129" viewBox="0 0 237 129" fill="none"> <path d="M228 1H9C4.58172 1 1 4.58172 1 9V91V104V127L20 111.483L228 112C232.4 ...

Tailwind's implementation of CSS Grid allows for the creation of a grid structure where specific cells can be selectively populated

I am currently using Tailwindcss with my Next.js application to showcase multiple images retrieved from a Supabase database in a grid layout. However, I am facing a problem where the images are being displayed in rows instead of columns within the grid whe ...

CSS inline-block element disregarding margins and padding

Recently, I've been delving into the world of CSS and trying out "display:inline-block" as an alternative to "display:float". The general consensus from documentation is that properties commonly used on block elements can also be applied to inline-blo ...

Struggling to locate the src/site/globals/site.variables file to customize the font-family in Semantic UI React. Is there an alternative method to achieve this?

I've been attempting to modify the overall font-family for my application, but so far, I haven't had any luck. Since I'm working with Semantic-UI-React, I initially thought that could be the issue. However, when I tried to locate src/site/g ...

What is the process for dynamically populating a select dropdown based on the selection made in another select dropdown?

I need to dynamically populate the second select box based on the option selected in the first select box. Here's what I have tried so far, but it doesn't seem to be working as expected. HTML: <form id="step1"> <p> Creat ...

What is the best method to enable horizontal scrolling within a div element by overflowing its content?

My layout looks like this: <div id='container'> <div id='cont1' style='width:150px'>Content 1</div> <div id='cont2' style='width:150px'>Content 2</div> <div id=' ...

Is it possible to use multiple stylesheets with PhoneGap?

Currently, I am storing a stylesheet (CSS file) from the web server into device storage using the file system access method provided in the phonegap guide for File. I need to apply this stylesheet to my app from the device storage, and will be updating the ...

Ways to avoid automatic error correction when running npm lint?

Utilizing the Vue CLI, I developed a Vue3 app with a Prettier/Eslint configuration. Upon making changes to the main.ts file as shown below: import { createApp } from "vue"; import App from "./App.vue"; import router from "./router& ...

Transforming a horizontal list into a vertical one

What might be causing the message list to display horizontally in the admin dashboard, but vertically on other pages? .user-list{ display:inline; } .users header, .users-list a{ display: flex; align-items: center; padding-bottom: 20px; border-bottom: 1px s ...

Transforming a few pages to be generated server-side using Vue3

I am exploring ways to convert just two pages within my Vue3 project to be server-side rendered for improved SEO without having to switch to Nuxt.js. Your assistance in this matter would be greatly appreciated. My attempt to use Node.js configurations ba ...

Is it possible to apply a PNG icon collection using CSS?

Throughout various examples, jQuery plugins, and CSS-styled HTMLElements, I have observed the setting of icons on an image with multiple options. I am eager to implement this practice in my own apps. https://i.sstatic.net/WDCH3.png For instance, in the ...

When hovering over the sidebar, it smoothly slides out. (ideally using only CSS)

I've been spending my free time working on a website, but I'm struggling to figure out how to create a navigation similar to the one featured on FontShop's site: . I want it so that when you hover over their logo (or in my case, a blank rect ...

the child div within a Bootstrap 3 column is not being properly styled

Experiencing a strange issue with a relative div within a column in Bootstrap 3. Here's my code: <div class="col-lg-6"> <div class="large" style="background: url(img/bg.jpg);"> <h1 class="grid-header">Content 1</h1> ...

Having difficulty selecting a nested ul with CSS, I'm currently exploring different options

For the website I am working on, I have a cms system that is responsible for rendering menus. My current task involves selecting the children of the parent menu items, and here is how the code is being generated: <div class="menu"> <ul> ...

I am wondering why the vue-router is only changing the URL and not displaying the corresponding view component

I've encountered an issue where the vue-router is only toggling the URL when clicking on the navigation link, but not updating the view component. Surprisingly, there are no apparent errors in my code. Despite this, the console indicates that the Home ...

What is the best way to retrieve a linked filter in Vue from another?

I have created a file to store filters linked to my Vue object, and it is being imported into my App.js. One of the filters I have needs to use another filter: Vue.filter('formatDateTime', value => { if (value) return moment(String(value ...

Apply a unique CSS class to the first two elements, then skip the following two elements, and continue this pattern using ngFor in Angular and flex styling

Here is a code snippet that displays a list of products using *ngFor in Angular: <div class="container-products"> <div class="item-product" *ngFor="let product of ['product A', 'product B', 'prod ...

Tips for styling row headers in Vaadin with CSS

I am in need of assistance with a problem I am facing. Recently, I started learning the Vaadin framework and I am trying to apply CSS to the row headers (first cell of each row), but so far I have not had any success. Below is the code that I have been usi ...

Recovering imported Objects in Vuex post-filtering

Dealing with Vuex and an imported .js file is proving to be a challenge once again. I have two buttons in my application. The first button initializes my store and imports a joke.js file containing jokes, which are then loaded into the state: jokes:[]. The ...

Display the field names from a MySQL table on a web page

Is there a way to show all column names from a MySQL table on a webpage if only the table name is known? ...