What could be causing the data to not update even after being updated in the mounted() lifecycle hook?

When the string subtitleURL is not empty, I aim to adjust the fill value. My approach involves updating the value of subtitleURL when one is provided. It appears to log correctly in the mounted method.

<template>
  <button class="glottis_button" @click="clickTest">
    <svg
      width="18"
      height="20"
      viewBox="0 0 18 20"
      fill="none"
      xmlns="http://www.w3.org/2000/svg"
    >
      <path
        d="M17.42 16.03C17.42 16.65 17.31 17.11 17.09 17.41C16.87 17.71 16.52 18 16.04 18.28C15.76 18.44 15.41 18.6 14.99 18.76C14.57 18.9 14.11 19.02 13.61 19.12C13.11 19.24 12.58 19.33 12.02 19.39C11.48 19.45 10.94 19.48 10.4 19.48C8.96 19.48 7.61 19.29 6.35 18.91C5.09 18.51 3.99 17.92 3.05 17.14C2.13 16.34 1.4 15.35 0.86 14.17C0.32 12.99 0.05 11.62 0.05 10.06C...
      
    </svg>
  </button>
</template>

<script>
export default {
  name: "GlottisRecorder",
  data() {
    return {
      subtitleURL: "",
      subtitleArray: [],
      clicked: false,
    };
  },
  methods: {

    clickTest: function () {
      console.log(this.subtitleURL)
    }
  },
  mounted() {
    chrome.extension.onMessage.addListener(async function (
      message,
      sender,
      sendResponse
    ) {
      if (
        message.tabURL === document.URL &&
        this.subtitleURL !== message.subtitleURL
      ) {
        console.log("MESSAGE useeffect", message, sender);
        this.subtitleURL = message.subtitleURL;
        console.log(this.subtitleURL)

      }
    });
  },
  computed: {
    computedFill: function () {
    console.log(this.subtitleArray.length < 1 ? "#fff" : this.clicked === false ? "#1eb4d4" : "#9ef7b9")
      return this.subtitleURL === "" ? "#fff" : this.clicked === false ? "#1eb4d4" : "#9ef7b9";
    },
  }
  
  

};
</script>

Although the value is correct when this.subtitleValue is logged on the console, clicking the button still results in the default value being displayed.

Answer №1

The event listener is incorporated using a traditional function, which means that this does not refer to your Vue component instance.

To ensure the correct context is captured automatically, switch to using an arrow function:

chrome.extension.onMessage.addListener(async (message, sender, sendResponse) => {
  this.subtitleURL = message.subtitleURL;
})

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

Is it possible to transfer a child from one parent to another using JavaScript?

I need help with moving an element from one parent to another using JavaScript, preferably using the jQuery library. Original code: <div id = "div1"> <p id = "paragraph"> Lorem ipsum dolor sit amet, adipiscing pellentesque egestas. &l ...

What approach should be taken to effectively utilize the Vuetify skeleton in a Nuxt project?

I'm relatively new to Vue, Nuxt, and Vuetify and all their components. Currently, I'm developing a Nuxt project with Vuetify and I want to implement the skeleton loader, but I'm facing some challenges. Right now, I'm using the following ...

Is it possible to deactivate the video feature within the Programmable Video API's P2P connection offered by Twilio?

Can we utilize the programmable video API from Twilio to initiate an audio call through a P2P connection? Our goal is to enable both audio and video calls utilizing the Programmable Video API with a P2P connection. Is this achievable? ...

Prevent the box from closing automatically after submitting a form using jQuery

Despite my efforts to keep a simple dialog box open after submitting a form, it continues to close. I tried using the preventDefault method in jQuery, but it didn't solve the issue. Below is the code snippet: $(document).ready(function(e) { $(&apo ...

Choose the date that is exactly 48 hours from now

I need to implement a date picker that shows today's date and the next 2 days in a select component. Can this be achieved using JavaScript and jQuery? While I came across a similar post on the topic, I specifically want it to work with a date picker: ...

Uncertain about how to display HTML provided as a string from a GET request

After making a get request and receiving data, I encountered a property like the following: <pre class="some-class" spellcheck="false">&lt;p&gt;SomeText&lt;/p&gt; &lt;div class="article__image"&gt ...

Tired of the premium content on Medium.com. How can I conceal premium posts that are aimed at a specific class within a parent element?

I have noticed that all Premium posts contain an element with the class ="svgIcon-use" <svg class="svgIcon-use" width="15" height="15" viewBox="0 0 15 15" style=""><path d="M7.438 2.324c.034-.099.090 123 0l1.2 3.53a.29.29 0 0 0 .26.19h3.884c.11 0 ...

Tips for maintaining circular references when converting a JavaScript object to JSON string format

Currently, I am developing a filetree-like object that has a unique structure: var myObject = { 'name':'foo', 'contains': {'thisObject': myObject,'parentObject': /* the object that contains the cur ...

What is the best way to show two icons next to each other within a React component?

For my school project, I am working on developing an app and encountering a challenge. I have been trying to display two icons side by side on a screen using React (I've been learning React for 5 months but still consider myself a beginner). However, ...

Toggle the visibility of an element using a checkbox in JavaScript

In my scenario, there are 8 checkboxes where only one is checked by default. If you click on the unchecked checkboxes twice, the table linked to them will hide. Ideally, I want the unchecked checkboxes to be hidden by default and the checked one to be sh ...

Maintaining the footer and bottom section of the webpage

I am facing an issue with the footer on my website . I want the footer to always stay at the bottom of the page, even when I dynamically inject HTML code using JavaScript. The footer displays correctly on the main page , but it does not work as intended on ...

What is the best way to remove empty arrays from a larger array?

I have successfully managed to extract the array containing the product (filtered by an ID), but I am encountering empty arrays in the output as well. Below is the snippet of code I used for this example: var category = [{ "reference": 'BC-ENF ...

Craft a FormType showcase using the dynamic duo of Ajax and Jquery

If you'd like to check out the code snippet for reference, please visit: This view serves as the canvas where innovation blends with execution. As I embark on this journey towards dynamic form creation, every step counts. Thank you for being part of ...

Reduce brightness in JavaScript - adjust downward

I stumbled upon this code snippet on Stack Overflow: function increase_brightness(hex, percent){ var r = parseInt(hex.substr(1, 2), 16), g = parseInt(hex.substr(3, 2), 16), b = parseInt(hex.substr(5, 2), 16); return '#' ...

Using AngularJS to access JSON files through the $http service

I'm experiencing difficulties reading data from my test.json file using the #http service. I have everything set up on a xampp localhost, but I can't seem to figure out what's going wrong. Here's the JavaScript code. Thank you in advanc ...

Image embedded in Discord

I'm encountering an issue with my discord bot. I'd like the image to be displayed within the embed instead of showing up as a separate message. Can someone help me troubleshoot this problem? Below is the code I'm currently using: function f ...

React client side componentDidMount function encountering issues (server side rendering)

Greetings to the Stackoverflow community Apologies in advance if my explanation is not clear enough When a user directly types a URL, the server makes a request, fetches the corresponding data, and renders it on the screen flawlessly. However, when a us ...

What is the best way to display a single object from an array once data is retrieved from an API

Having trouble mapping out the individual driver from the JSON array that was generated. Can't seem to get it into a list or anything. JSON: [{"driver_id":"0-ikkpyhyjg9","location":{"latitude":null,"longit ...

The React Router fails to redirect to the correct page, instead leading to an empty page without any errors displayed

The React Router "Link" feature isn't functioning properly at the moment. Imagine we start at: "localhost:3000/api/pages". Upon clicking the dropdown item, the link becomes: "localhost:3000/api/pages/api/pages/abc". It should actually be: "localhos ...

Establishing a connection to a MySQL database in passport.js to deserialize user

Is there a way to handle deserialization of a User in passport.js when connected to a database? It was functioning properly with an array, but encountered issues once linked to the database. What steps can be taken to resolve this problem? Error: Failed ...