Issue with Vuex reactivity when dealing with a list nestled inside an object

I attempted to build a scheduler app using vuex. To set up the scheduling, I utilized a state property named "scheduleController" which is structured as an object shown below.

const state = {
    scheduleController: {
    2020: {
        11: {
            month: "November",
            count: [{
                days: [],
                timers: [{
                    from: null,
                    interval: null,
                    to: null
                }]
            }]
        }
        12: {
            month: "December",
            ...
        }
    }
}
}

Each time I click on an add button, another object should be added to the timers array to create multiple schedules for the selected days in the month. This is achieved through the following mutation:

addTimer: (state, indices) => {
    let timer = {
        from: null,
        to: null,
        interval: null,
    };  
    //indices[0] represents the year, indices[1] represents the month-index 
    //and indices[2] represents the nth element of count array where
    //an empty timer needs to be added. 
    Vue.set(state.scheduleController[indices[0]][indices[1]].count[indices[2]].timers, 1, timer)
}

The state changes as expected but it lacks reactivity. I have to navigate to another view and then return to witness the state change. How can I ensure it is reactive?

Initially, in the code above, I used

state.scheduleController[indices[0]][indices[1]].count[indices[2]].timers.push(timer)

instead of the Vue.set function. However, even after making that change based on this link, it still did not work. Thank you. https://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats

Answer №1

Figured it out

    initiateTimer: (currentState, position) => {
        let newTimer = {
            start: null,
            end: null,
            duration: null,
        };


    let updatedScheduleState = JSON.parse(JSON.stringify(currentState.scheduleState))

    updatedScheduleState[position[0]][position[1]].counter[position[2]].timers.push(newTimer)

    currentState.scheduleState = updatedScheduleState

    },

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

What methods can I use in Mocha with Sinon for testing multiple callbacks?

My code involves a function with asynchronous operations and multiple callbacks var f = (cb1, cb2) => { return new Promise((resolve, reject) => { /* ... */ }); }; During testing, I wanted to use sinon to create spies var cb1Spy = sinon.spy(); va ...

The API is now configured to provide a single result rather than returning multiple results in response to an 11ty/elevent

In my 11ty project (static site generator), I am fetching property data and then using it in a Promise.all call to: fetch all property images fetch tenancy application URLs After that, I combine these into one array named allData for my pages. The image ...

The next image is crashing on every device except for mine

On my device, the next image works perfectly, but it crashes on every other device. Here is the JSX code: <div className="img-container-homePage"> <Image src="/catI.jpg" alt="Cat" layout="fill" /> < ...

Laravel - AutoComplete Feature: Populate Input Field with JSON Data Instead of Suggestions

I've been working on adding search functionality to my Laravel 5.4 project, but I'm running into an issue. The suggestions show up fine in the dropdown menu, but when I select one, the input field gets filled with JSON data instead of the suggest ...

Utilizing Node.js and express to promptly address client requests while proceeding with tasks in the nextTick

I am looking to optimize server performance by separating high CPU consuming tasks from the user experience: ./main.js: var express = require('express'); var Test = require('./resources/test'); var http = require('http'); va ...

Converting a Base64 URL to an image object: A step-by-step guide

I currently have a base64 URL in the following format: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAA My objective is to convert this into an image file with the following properties: [File] 0: File lastModified: 1559126658701 lastModifiedDate: Wed M ...

Assigning the style of the parent element based on the child element

Currently, I am attempting to develop a customized dropdown field and here is the code I have come up with: const list = document.querySelector('.list'); const listItems = list.getElementsByTagName('p'); document.querySelector('# ...

The rendering of React child components is not functioning

Hi there! I am new to using React and I am currently facing an issue with rendering components. My problem is that only the parent component is being displayed, and the child components are not being recognized by React. Here is the snippet of code from m ...

Using a jQuery function to search for values in an array

This is the format of my structure: var var1 = { array1 : ['value1','value2', ...], array2 : ['value3','value4', ...] ... }; I am looking for a JavaScript function that can search for values within ...

How can you pick the element that is nearest to the top of a window?

My goal is to have a fixed list of page sections on the side that highlights the link of the section you're currently viewing as you scroll through the page. This is the code I've come up with: $(document).scroll(function(){ $allSections = $(&a ...

Executing a parent class function within an asynchronous function: a step-by-step guide

I'm facing an issue where I need to execute the pushBulkData function within a stream reading event. I attempted to do so with the following: this.pushBulkData(row, importData); However, I understand that this won't work due to the separate sc ...

Angular2: Leveraging click events to manage CSS classes on elements

I am currently developing a web app using Angular 2. I'm facing an issue where the active element should receive an extra CSS class when clicked, but adding the ":active" CSS attribute with a custom class doesn't work as expected. The ":focus" pr ...

Executing two JavaScript functions simultaneously by clicking one button yields a contrasting outcome compared to running them separately

Presently, I am utilizing webcam.js to capture a still image and then use JavaScript code to pass it to a hidden variable for retrieval in the ASP.NET code behind where I can save it to the database. Here is the JavaScript code snippet: <script src="we ...

Can two adjacent VueJS components be rendered simultaneously?

Two custom VueJS components were created and the goal is to place them side by side, as shown below: <div id="app"> <x-component /> <y-component /> </div> ... new Vue({ el: '#app', components: { ...

Custom HTML on Joomla causing carousel to vanish

I've encountered an issue with my Joomla website where my carousel images are disappearing. I have a code snippet from W3 Schools for an automatic banner that works perfectly fine when opened in a browser using Notepad++, but when inserted into a cust ...

What are the best ways to establish communication among JavaScript modules?

I have multiple modules in my application, each with their own responsibilities, but I'm unclear on how they should communicate with one another. What is the best way for modules to interact with each other? How can modules notify or listen to e ...

What is the significance of employing the `var` keyword in Vue.js?

As I dive into tutorials and browse through code snippets while immersing myself in learning this framework, I've noticed a common trend - the use of var for declarations. This practice seems prevalent across resources, including the official Vue docu ...

vue Delay loading the component content until it is requested

As a newcomer to Vue, I have come across a problem recently. Let's say I have numerous popups with heavy content that is only needed when requested. I realized that my initial approach was incorrect (using Vue Router to render the content, but it didn ...

Unexpected security question arising from vue-CLI output

As I work on my Vue.js application, I've encountered a sudden issue with vue-cli producing error messages that raise concerns about potential malicious dependencies. Executing npm run serve (vue-cli serve) appears to be successful, but it generates m ...

Put the browser into offline mode using JavaScript

Currently, I am utilizing selenium for application testing purposes. Although I typically start my browser in the usual manner, there comes a point where I must transition to offline mode. I have come across various sources indicating that switching to off ...