Is it possible to have a getter in vuex dynamically update when another getter's value changes?

Within my vuex store, I have this getter that retrieves authority_count:

authority_count (state, getters) {
      return getters.dataView?.getUint16(4, true) || state.pack.authority_count;
}

I want authority_count to default to state.pack.authority_count when dataView is null. However, once a file is loaded on the page, I want authority_count to be recalculated after the loading process is complete and dataView is populated with the file's data. How can I achieve this functionality?

The issue arises when I ensure getters.dataView returns a DataView object, as authority_count still defaults to state.pack.authority_count.


I am using a file input to extract binary data from files and load it into an arrayBuffer accessed by the dataView getter.

Link to loading arrayBuffer code

Link to loading dataView code

Logs related to authority_count:

[HMR] Waiting for update signal from WDS...
getters.dataView:  null
state.pack.authority_count:  5

Answer №1

Identified are two key issues within the project provided:

  1. Any alterations to the Vuex state, such as arrayBuffer, should be exclusively carried out within a Vuex mutation:
mutations: {
  setArrayBuffer(state, payload) {
    state.arrayBuffer = payload;
  }
},

In order to invoke this mutation, use the following snippet:

reader.onload = (event) => {
  this.$store.commit('setArrayBuffer', event.target.result);
}
  1. The component's arrayBuffer ought to be computed in order to maintain synchronization with Vuex:
data () {
  return {
    pack: this.$store.state.pack
  }
},
computed: {
  arrayBuffer() { // Alternatively, you can utilize `mapState` for the same functionality
    return this.$store.state.arrayBuffer
  },
  buffer () {
    return [...new Uint8Array(this.arrayBuffer)]
      .map (b => b.toString(16).toUpperCase().padStart(2, '0'));
  }
},

If you desire similar synchronization for pack, you will need to implement the same approach.

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

I'm looking for a method to dynamically update my component without the need to refresh the entire page. Can

I created a Vue component that constructs the card and within it, I have another component to which I pass data using a prop. Currently, I am using Swal as a popup to add a new project to the database. However, after adding the project, I have to manually ...

Ways to adjust the position of a DIV based on its index value

I'm currently working on a unique project that involves creating a triangular grid using HTML and CSS. The challenge I am facing is offsetting each triangle in the grid to the left by increasing amounts so they fit seamlessly next to one another. Righ ...

Retrieving multiple lines of text from the database and populating a TextArea

I am encountering an issue with multi line text saved in a MySql database as VARCHAR 255. When I retrieve and process it using the nl2br PHP function, it displays correctly (with multiple lines). However, when I retrieve the multi line text from the databa ...

Guide on uploading files using Vue.js2 and Laravel 5.4

I'm currently attempting to implement an image upload feature using Laravel for the backend and Vue.js2 for the frontend. Here are snippets from my code: addUser() { let formData = new FormData(); formData.append('fullname', this.n ...

What is the best way to invoke a function from one controller in angularjs to another controller?

I am trying to implement a solution where I need to call the Listing function of testController from userController. I have placed a marker (//) in the code snippet below to indicate where I want to make this function call. This implementation is being d ...

Discovering the generic type from an optional parameter within a constructor

Looking to implement an optional parameter within a constructor, where the type is automatically determined based on the property's type. However, when no argument is provided, TypeScript defaults to the type "unknown" rather than inferring it as "und ...

Issue encountered while attempting to host an ejs file using Express in Node.js

I'm encountering an issue while attempting to serve an .ejs file using express. The error I'm getting is as follows: Error: No default engine was specified and no extension was provided. at new View (C:\development\dashboard& ...

How to use jQuery to target the second or any desired div element with a specific class

I'm having trouble with something seemingly simple Let's say we are looking for a class called .contentdiv, for example. I am trying to target the second or nth occurrence of the .contentdiv class in a document and retrieve the HTML of that spe ...

Improprove the efficiency of flattening an array containing nested JSON objects using JavaScript

Supposed to be an array structured like this: [ { "key_set1": { int_val: 3, arr_val: [ 1, 3, 4 ] } }, { "key_set2": { stri ...

Is a 'Virtual DOM' included in React Native's architecture?

According to the ReactJS wiki page on Virtual DOM: React uses an in-memory cache of data structures to efficiently compute differences and update the displayed DOM in the browser. This allows developers to write code as if the entire page is re-rendered ...

The issue lies in the error code TS2315 which states that the type 'observable' is not a generic

I keep encountering an error message that says 'observable is not generic' while importing files. I am working on implementing CRUD operations in Angular 7, where I have created two components for adding and listing employees. The functions for c ...

Creating a server that is exclusive to the application in Node.js/npm or altering the response body of outgoing requests

As I work on developing a node app, the need for a server that can be used internally by the app has become apparent. In my attempts to create a server without listening to a port, I have hit a roadblock where further actions seem impossible: let http = ...

view multiple HTML documents simultaneously in a single browser tab

Currently, I am in the process of building a wiki and have included tables in various sections. I want to showcase these tables on the main page as well. Rather than constantly copying and pasting them, I'm looking for a way to have the main page auto ...

Performing subtraction on two arrays in Javascript

Is there a way to eliminate all duplicate elements from one array list that are also present in another array list using the .filter() method of ES6 javascript? I am open to solutions in jQuery as well, but they must be compatible with IE11+ and Chrome fo ...

Struggling to achieve success in redirecting with passport for Facebook

For a "todolist" web application that utilizes passport-facebook for third party authentication, the following code is implemented: passport.use(new FacebookStrategy({ clientID: '566950043453498', clientSecret: '555022a61da40afc8ead59 ...

Is it feasible to access reference images contained within a zip/tar file using html/javascript with the help of a library?

Although this question may appear similar to others, there is a key distinction that sets it apart. In search of request efficiency rather than space efficiency, lazy loading could be the solution. However, in situations where content needs to be quickly ...

When utilizing scoped slots in BootstrapVue, you may encounter an error stating "Property or method 'data' is not defined."

Greetings! I am currently in the process of learning how to utilize BootstrapVue, and I decided to reference an example from the official BootstrapVue documentation. <template> <div> <b-table :fields="fields" :items="items" foot-clone ...

What is the best way to display the HTML content being received from the database in the DOM?

I have received an object in this format [ { "BET": 57630343, "CUSTOMER": 181645, "SPORT": "MLB", "XX_FILL OPEN": "<button class=\"btn\" onclick=\"fillOpen(57630343)\">Fill Open</button>", "XX_VIEW": n ...

Why is my Vue/MySQL Database not showing up online, even though it is accessible locally?

While my application runs smoothly locally, I encounter an issue when deploying to the Heroku server. The page contents linked to the MySQL Database fail to display without any CORS errors or fetch call issues in Chrome Dev Tools. The page loads, but remai ...

Sending a file from a VueJS application to a Laravel API

Currently, I am facing an issue while trying to upload an Excel sheet file from a front-end application built with VueJS to an API constructed with Laravel 5.5. The form request validation is indicating that "The file field is required", resulting in the f ...