Surprising outcome of Vue

As a newcomer to vue.js, I am struggling with a side effect issue in a computed property. The unexpected side effect error is popping up when running the code below, and ESlint is pointing it out in the console. I understand the concept of side effects, but I'm unsure how to resolve this specific error. Any suggestions?

export default {
    name: "Repaid",
    components: {
        VueSlideBar
    },
    data() {
        return {
            response: {},
            slider: {
                lineHeight: 8,
                value: 2000,
                data: []
            },
            days: {},
            monthCounts: [5],
            currentMonthCount: 5,
            isLoading: false,
            errorMessage: "",
            validationError: ""
        };
    },
    computed: {
        finalPrice() {
            const index = this.monthCounts.indexOf(this.currentMonthCount); 
            this.days = Object.keys(this.response.prices)[index]; // This line causes the side effect
            const payment = this.response.prices[this.days][this.slider.value].schedule[0].amount;
        }
    },

I have come across suggestions to add a method like slide() to my 'days' variable in order to mutate the original object or move them into methods instead.

EDIT

I have refactored my calculations by moving them into methods and triggering a function within the computed property. Here is the updated code:

 methods: {
    showPeriod() {
        const index = this.monthCounts.indexOf(this.currentMonthCount);
        this.days = Object.keys(this.response.prices)[index];
        const payment = this.response.prices[this.days][this.slider.value].schedule[0].amount;
        return "R$ " + payment;
    },
},
computed: {
    finalPrice() {
        if (!this.response.prices || this.monthCounts === []) {
            return "";
        }
        return this.showPeriod();
    }
},

Answer №1

The source of this information is actually from the "eslint-plugin-vue," and you can access the specific rule at the following link:

If you prefer, you have the option to override this rule in your eslint rules file or simply disable eslint for a particular line like this:

this.days = Object.keys(this.response.prices)[index]; // eslint-disable-line

--
Additionally, unrelated to your initial query, ensure that your computed function returns a value.

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

Delayed Passport Session Login

Every time I try to log in, my Express app loads very slowly... I've implemented Passport and Express Validator, but there are no errors. However, the login process for some users is extremely slow. Can anyone offer assistance? Below is a snippet o ...

Can Three.js be used to create a compact canvas?

I've successfully implemented a three.js scene on my website where users can drag to rotate an object. However, I don't want the scene to take up the entire webpage. I tried adjusting the field parameters with the following code: renderer.setSiz ...

How can I tally the number of channels within a specific category using discord.js?

I'm in the process of creating a bot that can provide me with the number of channels within a specific category. if (strMessage === "!outline") { var outlineSize; message.reply("There are " + outlineSize + " outlines curr ...

Unpredictable preset inline styles for HTML input elements

While developing a full-stack MERN application, I encountered an unusual issue when inspecting my React UI in Chrome DevTools. If any of these dependencies are playing a role, below are the ones installed that might be contributing to this problem: Tail ...

Enhance the editing capabilities of the Json data form

https://i.stack.imgur.com/YZIjb.png My goal is to enhance a form for editing json data by moving beyond the typical <textarea /> tag and making it more user-friendly. Are there any tools available that can help improve the form's usability? Add ...

Utilizing Laravel and Jquery UI for asynchronous communication with the server, passing data from a post request to

I recently constructed a basic Jquery UI slider: $("#sliderNumCh").slider({ range: "min", min: 0, max: 20, step: 1, value: 20, change : function(e, slider){ $('#sliderAppendNumCh'). ...

Update the array state based on the selection of checkboxes and user input in real-time

In my current project using react js, I am working on a UI development task where I need to create a dynamic table based on data fetched from an API. Each row in the table includes a checkbox and a text input field that are dynamically generated. My goal i ...

Transitioning from Laravel blade to Vue.js: Managing tag handling techniques

Currently, I am working on a Laravel application that utilizes blade templates. My goal is to gradually transition these templates into Vue.js, so for a period of time, I plan to use both Blade and Vue.js in my code simultaneously until the full migration ...

Ways to detect when the window printing process has been completed

Within my application, I attempted to generate a voucher page for the user using the following code: var htm ="<div>Voucher Details</div>"; $('#divprint').html(htm); window.setTimeout('window.print()',2000); The &apo ...

Error encountered while trying to upload a file using PHP on a hosting platform

Hey everyone, I'm feeling really frustrated right now because I'm facing a file upload error in my PHP code. The issue is that the file is not getting uploaded to my folder and no error messages are being displayed. I've been working on cr ...

Using ngrx to automatically update data upon subscription

Background The technology stack I am using for my application includes Angular 4.x, ngrx 4.x, and rxjs 5.4.x. Data is retrieved from a websocket as well as a RESTful API in order to share it between multiple components through ngrx. Currently, data is ref ...

Struggling to incorporate blocks into Jade for Express. Encountering errors like "Max Stack Size Exceeded" and issues with setHeader

I am currently using Express along with a simple express-generator server that I created. My first task was to focus on creating the view layout and extending the index page, but unfortunately, I have encountered some challenges. Throughout my process, I& ...

What is the best way to retrieve an array of objects from Firebase?

I am looking to retrieve an array of objects containing sources from Firebase, organized by category. The structure of my Firebase data is as follows: view image here Each authenticated user has their own array of sources with security rules for the datab ...

Run a Javascript function two seconds after initiating

My goal is to implement a delay in JavaScript using either setInterval or setTimeout, but I am facing an issue where the primary element is getting removed. Code that works fine without Javascript delay: const selectAllWithAttributeAdStatus = document. ...

When extracting the host URL and destination URL

When making an Ajax POST call to retrieve data from a file on a different server, I am encountering an issue where my host URL is being added to the destination URL for my AJAX request. Host URL: 192.168.1.2 Destination URL: 192.168.1.7/var/www/html/pfe/ ...

Can dark mode be activated and maintained across multiple pages using just one JavaScript file?

I'm facing an issue with maintaining a dark mode across multiple web pages. I tried adding a JavaScript script to all my HTML files, but it didn't work as expected. Then, I experimented by adding the script to just one HTML file, and while it suc ...

Is there a way to display these panels in a scrollable table layout?

My aim is to replicate this specific styling: https://i.stack.imgur.com/jz5lm.png This type of table shown above is being dynamically imported via Redux: class LocationList extends React.Component { componentDidMount() { this.props.fetchLocations( ...

Steps to address vulnerabilities in @vue/cli

I am currently working on a vuejs-3 project and aiming for 0 vulnerabilities. However, upon running npm install, I encounter 48 vulnerabilities with the current version of node and npm. Even after trying npm audit fix --force, the issue persists. Can anyon ...

Strategies for increasing a value within an asynchronous function

I'm facing an issue with incrementing the variable loopVal inside a promise. I've tried to increment it without success. Any ideas on how to make this work? const hi = function(delay) { let loopVal = 1; return new Promise((resolve, reject) ...

What is the best way to include an image link in a JSON file using Nuxt.js?

I am working with nuxtjs and I have a file named data.json in the root directory. I am trying to add an image link in this JSON file, but it is not recognizing it. Here is my code: { "id": 1, "cardImg": "./assets/images/ima ...