Transferring information to a Vue child component without the need to reload it

I have a page with audio visualization done within a component. The component solely displays content, while the audio logic is handled by the parent component. To keep it brief, I am facing an issue where I need to pass the AnalyserNode to the child component for real-time frequency analysis using Three.js render-loop in the WorkletModule.

In my parent component, it looks something like this:

<template>
    ...
    <WorkletModule
      :analyser="analyser"
      :key="componentKey"
    />
    ...
</template>

...

data() {
    return {
      componentKey: 0,
      analyser: null,
    };

methods: {
     startRecording() {
      this.componentKey++
      ..

      this.analyser = audioContext.createAnalyser();
      ..
     }
}

and my WorkletModule.vue code is as follows:

  props: {
    analyser: {
      type: AnalyserNode
    },
  },

  mounted() {
      //initialize graphics, etc..
  }

//I need to do something like this within the render loop:
this.analyser.getByteFrequencyData(soundData);

Although the above implementation works, each call to startRecording() causes the WorkletModule to remount, causing flickering and clearing the canvas. Removing ":key="componentKey" prevents updates, leaving the analyzer reference in the prop as null, which is not desired. Is there a way to pass the analyzer object without using a prop?

I require access to the analyzer value in each frame of the render-loop. While emitting an event from the WorkletModule’s render loop to trigger another event in the parent that sends updated values back to the child is a solution, it seems inelegant. Are there better alternatives?

Answer №1

This situation has me completely baffled:

After removing the ":key="componentKey" it remains unaltered, indicating that the connection to the analyzer in the WorkletModule/prop always remains null

This is unexpected as props are typically updated without requiring a re-render. However, utilizing a key causes the component to be completely destroyed and reconstructed with each key update.

You could monitor your props updates using a watcher.

In the provided example, I pass a prop to a child component, transitioning from null to a numerical value. The component is rendered only once, with the prop updating as anticipated. By adding a watcher, I am able to track every update of the mentioned prop and implement any necessary logic based on that. You might want to explore this approach to ascertain whether the prop truly remains static or if there might be an oversight in your implementation (potentially stemming from undisclosed code snippet influencing your perception).

const test = Vue.component("test", {
  props: ["prop1"],
  methods: {
    getProp() {
      console.log(this.prop1);
    }
  },
  created() {
    console.log("created"); // only called once
    setInterval(this.getProp, 2000);
  },
  watch: {
    prop1(newVal, oldVal) {
      console.log("watcher:", newVal, oldVal);
      // do something
    }
  },
  template: "<div>test</div>"
});

new Vue({
  el: "#app",
  components: {
    test
  },
  data: {
    toProp: null
  },
  methods: {
    setProp() {
      this.toProp = new Date().valueOf();
    }
  },
  created() {
    setInterval(this.setProp, 6000);
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <test :prop1="toProp" />
</div>

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

How about implementing Foreign Key with getters and setters in Sequelize?

Hello there! I recently started working with sequelize and ran into an issue. I added getters and setters to my model, but when I tried adding a foreign key, it didn't get created. Strangely enough, the foreign key only appeared after I removed the ge ...

Encountering issues with AngularJS number filter when integrating it with UI grid

When using the angularjs number filter in angular-ui-grid, I am facing an issue. The filter works perfectly fine within the grid, but when I export the grid to csv and open it in Excel, the formatting is not maintained. I have included the filter in the e ...

Traversing a JSON object in JavaScript and comparing its key-value pairs with a specific pattern

When parsing a json object and comparing it against a specified pattern using regular expressions, the approach involves calling a function to create a new user account if the pattern is matched, otherwise, another function is called to display existing us ...

The function for sending an AJAX post request to a file is unresponsive

I've been struggling with this problem for more than a week now and I just can't seem to find the solution, particularly because no error messages are being displayed. My goal is to send a single variable to a local file using jQuery AJAX. Here& ...

JavaScript - Monitoring changes to a node before they occur

Using MutationObserver, I am able to detect node insertion in a tree, however, it runs after the node is inserted. If I need to trigger an event before the node is actually inserted, what steps should I take? For example: // create a new observer instan ...

The linkType setting from MiniCssExtractPlugin and html-webpack-link-type-plugin both seem to be malfunctioning

I have provided my webpack configuration details below: const LinkTypePlugin = require('html-webpack-link-type-plugin').HtmlWebpackLinkTypePlugin; const path = require('path'); const webpack = require('webpack'); const FileMan ...

Having trouble uploading APK to Google Play Developer using Publisher API

Currently, I am in the process of developing a script using nodejs to upload my APK file to Google Play Developer through the Publishing API. However, I have encountered some issues with the upload process. Despite having a valid APK file, the upload is fa ...

Is it possible to set up an automatic redirection to the Identity Provider sign-in page when accessing a protected page in Next.js using Auth.js?

Currently in the process of developing a web platform utilizing [email protected] and Auth.js([email protected]). The provider has been configured with the given code, allowing successful signing in using the "Sign in" button. auth.ts import Ne ...

Using the incorrect file path in Vue Storybook

Recently, I began experimenting with Vue and encountered an issue related to using aliases for importing files in Storybook: In my project, there is a SvgIcon component: <template> <div> props from the icon: {{this.$props}} ...

Navigating a complex web: Djikstra algorithm applied to a multigraph

Encountering an issue while implementing Dijkstra's algorithm on a multigraph. The graph consists of nodes representing stops with information and connections to other stops (edges). However, the challenge arises when there are multiple bus options be ...

Tips for incorporating dynamic URLs in Next.js

In my current project using nextjs, I am dealing with fetching images via an API. Right now, I am receiving the "Full image path" (for example, "https://myurl.com/image/imagename.jpg") without any issue. However, I need to figure out how to fetch the image ...

What is the best way to adjust the content of a Bootstrap Column to be at the bottom of the column

Currently diving into the world of Bootstrap for my personal website, I'm encountering a challenge in aligning the content of my sidebar to the bottom. My quest for a solution led me through numerous threads without success. <!-- wordsmith: < ...

Switching icons in a Vue framework when hovering over multiple elements

I'm looking to update the icon of a specific element on hover using Vue instead of JS/jQuery. Currently, my code is changing the icons of all four elements when hovered over: HTML: <div class="col-md-3"> <div class="welcome-latest"> ...

What is the process for setting a specific version of Node for a project on my local machine?

I am currently facing an issue with setting up Node across multiple developers' machines for a project. The challenge lies in the fact that not all team members are experienced in Node or JavaScript, and we need to ensure that everyone has the correct ...

Error: The property value supplied for the calc() function is invalid

<App> includes both <Header> and <Content> components. I am attempting to calculate the height of <Content>, which is equal to the height of <App> minus the height of the header. // App.js import { useStyles } from "./Ap ...

Determine the availability of a distant website through AJAX requests

My website runs PHP's cURL to check the online status of 5 different URLs, however, this process tends to slow down the page load time significantly, especially if one of the sites being checked is not working. Someone suggested using jQuery's a ...

Using Meteor JS to save images in MongoDB

Looking to save an image uploaded via a file input into a database. Utilizing the redactor plugin to browse the image with the following code: $('.editor').redactor({ imageUpload:"/uploads" }); Once an image is selected or browsed, it is se ...

Maximize performance for jQuery datatables through advanced row grouping techniques

Recently, I started incorporating the jquery datatables plugin into my project and I must say it's quite impressive. However, I encountered a roadblock when attempting to implement row grouping on my own. Unfortunately, my solution is far from optimal ...

Creating a unique custom bottom position for the popover and ensuring it is fixed while allowing it to expand

Creating a customized popover similar to Bootstrap's popover, I have successfully implemented popovers in all directions except for the top direction. My challenge lies in fixing the popover at the bottom just above the button that triggers it, and en ...

Inter-class communication using TypeScript callbacks

Struggling with Typescript, I have encountered an issue while trying to send a callback from an angular controller to an angular service. Despite setting a break point at the beginning of the callback function using Chrome Dev Tools, it never gets triggere ...