Reactivity of dictionary object in Vue with Vuex

Currently, I am utilizing Vue along with Vuex to manage the state in my application.

Let's take a look at my store setup:

state: {
  nodes: {}
},
actions: {
  loadNodes ({ commit }, parameter) {
     axios.get('http://URL' + '/nodes', {
        params: {
          name: parameter['name'],
          type: parameter['type']
        }
      }).then((response) => {
        commit('nodeActivity', { uid: parameter['uid'], res: response.data })
    })
  }
},
mutations: {
  nodeActivity (state, data: {uid, res}) {
    Vue.set(state.nodes, data.uid, data.res)
  }
},
getters: {
  getNodes: state => state.nodes
}

The structure of nodes is currently a dictionary format as follows: { id: data }

Next, I have a component with a lifecycle hook:

created () {
    this.$store.dispatch('nodeActivity', { uid: this.$props.uid, name: this.$props.namepar, type: this.$props.typepar })
  } 

After making an API call and storing data asynchronously, everything functions correctly. However, I encounter an issue when trying to retrieve specific data based on uid, such as this.$store.getters.getNodes[0]. While this.$store.getters.getNodes works well, indicating that reactivity is maintained through Vue.set(..), attempting to create a new variable like

tmp = this.$store.getters.getNodes
and then accessing an index like tmp2 = tmp[0] does not result in reactivity. This behavior has left me puzzled and seeking advice on how to improve the design of my store.

Answer №1

After exploring various options, I stumbled upon a less than ideal approach. I implemented a watcher for a variable that fetches data from the store, somewhat like

const tmp = this.$store.getters.getNodes
. Whenever tmp undergoes a change, I trigger a notification and set the variable tmp2 = tmp[number] to ensure reactivity. Admittedly, I consider this solution a bit of a hack.

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

Presence detected: Discord bot appears online but is missing from members list

I am embarking on the journey of creating my first Discord bot. After installing NodeJS on my computer, I used the Discord developer tools to create the app, turned it into a bot, obtained the token, selected privileges, and added the bot to my server. I ...

Setting up initial random useState value in React without relying on useEffect

I'm trying to display two different DIVs randomly: either one or the other. To achieve this, I am using a random boolean state variable and rendering based on its value. For simplicity, let's attempt to show a BLUE sign in the blue box and a RED ...

How to Toggle the "active" Class Among Child Elements in vueJS 2

I'm interested in learning how to implement this functionality using vue.js. Currently, I am able to toggle the active class on a single item, but when I click on another item, the active class remains on the previous item. Here is my current setup: ...

How can I efficiently update child states within a parent class using ReactJS?

Exploring the parent component class Root extends React.Component { constructor(props) { super(props); this.state = { word: Words, }; } c ...

When is the best time to assign properties to mobx domain objects?

When it comes to tackling a specific problem in my react/mobx application, I'm facing challenges finding an optimal solution. Imagine having multiple boxes that need to be positioned on the screen based on various factors such as interdependency betwe ...

NW.js throwing an error due to an invalid regular expression in Javascript

I'm having an issue with a regular expression that works fine when tested online. ^(?![.])^[.a-zA-Z0-9]*(?<!\.)$ You can test it here: https://regex101.com/r/bomm5g/1/ However, when I incorporate the regex into my code, I encounter the fol ...

What is the method for setting the proxy URL in Webpack.config.js following the deployment of the application?

When running on my local machine, the React front-end is hosted on localhost:3000 while the Node/Express back-end is hosted on localhost:8080. Within the webpack.config.js file for the front-end, I utilize a proxy to enable the front-end to retrieve data ...

Sort through a group of objects full of other objects

Currently, I am dealing with an object that contains other objects and I am in need of a method to retrieve all objects that match a specific value. For instance, if I provide the main object, 'city', and 'London' as parameters, I would ...

I am facing an issue with Ajax in Laravel, as it is displaying a page not

I'm having trouble sending OTP with Laravel and Ajax. Whenever I try to call the Ajax function, it displays an error saying "Page not found"... HTML: ` <div id="first_step"> <div class="col-md-4" ...

Displaying information retrieved from an AJAX request onto a webpage

Utilizing AJAX, I am dynamically adding more articles to a list by triggering a button press. The response data from the AJAX call consists of article titles, authors, and up to three associated images. Below is the current code implementation for displayi ...

Issue with compatibility of jquery.ui.autocomplete.js on Internet Explorer 7

Having an issue with IE7 that shows an error message "'active.0' is null or not an object" on line 39, which reads: input.trigger("activate.autocomplete", [$.data(active[0], "originalObject")]); $("body").trigger("off.autocomplete"); This code ...

Error: TypeScript is flagging that you can only specify known properties in an object literal, and the property '...' does not exist in the type 'DeepPartial<Document>'

I've been working on building a basic controller API in NodeJS with TypeScript, but I'm encountering the error ts(2345) while trying to assign values to the model. This is my user model: import mongoose, {Schema} from 'mongoose' cons ...

Sharing information from child to parent controller in AngularJS through data push

I encountered an issue while attempting to push a cities object to an array in the parent controller. The error message displayed was "Cannot read property 'push' of undefined". Is there a solution to this problem? The ChildCtrl is embedded with ...

Error occurs in Query component when using a higher order component (HOC) due to element type

I've developed a custom higher-order component called withInfiniteScroll, designed to enable infinite scrolling functionality for a basic list of data. My aim is to integrate this HOC within Apollo's Query component, but I'm encountering an ...

Create a JavaScript function that generates 100 evenly spaced steps within a given range

I have been struggling to generate steps of 100 between two values, -0.1 and 0.1. Despite trying various methods, I have not yet achieved success. In this code snippet, there is an input range: $('#slider').change(function(){ var val = ($( ...

Complicated scenario involving distinct identifiers and dynamically generated items

I am facing an issue with a button that, when clicked, adds a new media item to a list. The problem is that it uses unique IDs that end up getting duplicated. I am looking for a solution to add some kind of anonymous number to the ID to prevent duplication ...

User missing in the transition from POST to GET

My journey with Node has just begun, and I decided to delve into the Rocket Rides demo available on GitHub. While exploring the pilot sign-up feature on the web app, I encountered a roadblock when trying to implement a similar functionality for passenger s ...

using regular expressions to find unclosed font tags that match on a single line

Even though regex is not typically recommended for parsing HTML, in this case we are dealing with a single line string input to a function. For example: <font color = "#ff0000"> hello </font>. I want the regex pattern to match only if the tag ...

When the length of the Array is 100, the function Math.min.apply(Math, Array) will produce a

Is it okay to use Math.min.apply when the length of the Array is 10? getAllIndexes(arr, val) { var indexes = [], i = -1; while ((i = arr.indexOf(val, i+1)) != -1){ indexes.push(i); } return indexes; } arrayMinIndex(array) { return this.getAllIndexes ...

Tips for utilizing the router link feature in Vuetify

I'm currently working on a login form using Vuetify and I'm trying to implement a 'forgot password' option. I want it so that when the user clicks on 'forgot password', it redirects them to the forgot password page. I have tri ...