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

Instructions for triggering the opening of an additional component upon the clicking of a button in Vue.js

My goal is to use Vue.js for designing purposes. I want the dashboard to open when I click on the login button, but I am facing challenges because I am new to Vue.js. Below are my codes and the error message: I am getting the following error: TypeError: C ...

What is the best way to trigger useEffect when the state being used within the effect is expected to change during the course of the effect's

Exploring the following code snippet: const [list, setList] = useState([]); const [curPage, setCurPage] = useState(0); const fetchItem = useCallback(async ()=>{ const data = await callAPI(); // data is an object setList(prev => [...prev, data]) ...

Is it possible to enforce strict typing for a property within an object that is declared as type 'any'?

In my code, I am dealing with a parent object of type 'any' that remains constant and cannot be changed. Within this context, I need to define a property for the parent object, but no matter what I try, it always ends up being loosely typed as &a ...

Mobile browser experiences video freezing when src is set through useState, yet it starts playing when triggered by a button click or when the video is set to M

Hey awesome folks at the StackOverflow Community, I'm currently encountering a perplexing issue with a video element in my web application. The video is supposed to play automatically on both desktop and mobile browsers. However, I've run into a ...

Slow execution of Bootstrap v4 modal display

It has come to my attention that the modals in Bootstrap show slower as the page's content increases. When the page is empty, it only takes less than 100ms to show. However, the time it takes significantly increases as more content is added to the pa ...

Implementing Conditional Attribute Addition in Vue.js

When developing in vue, I encountered this scenario: <v-dialog v-model="data_table.dialog"> I have a variable called is_mobile that is observable. My goal is to modify the tag above based on the value of is_mobile. For instance: <v-dialog v-mod ...

Which Client-Side JavaScript Frameworks Pair Seamlessly With Node.js, Express.js, and socket.io.js?

Currently, I am in the process of developing a web application utilizing Node.js, Express.js, and socket.io.js on the server side. Are there any front-end frameworks (such as Agility, Angular, Backbone, Closure, Dojo, Ember, GWT, jQuery, Knockback, Knocko ...

Getting an error response with a status of 200 when making a jQuery ajax call

After sending a jQuery ajax request to my express-powered node.js server, everything seemed to be in order. However, to my surprise, the response invoked the error callback instead of the success callback, despite receiving a status code of "200". It was d ...

Finding the index of a chosen option in Angular

Attempting to retrieve the index of the selected option from a select element using Angular. The Angular (4) and Ionic 3 frameworks are being utilized. The template structure is as follows: <ion-select [(ngModel)]="obj.city"> <ion-option ...

Error: The function Object.entries is not defined

Why does this error persist every time I attempt to start my Node.js/Express server? Does this issue relate to the latest ES7 standards? What requirements must be met in order to run an application utilizing these advanced functionalities? ...

Setting the state to an array of objects in React: A beginner's guide

Currently, I am developing a flashcard application using React. To store the data entered by the user, I have initialized my state as an array of objects that can hold up to 10 terms and definitions at a time: state = { allTerms: [ { ...

Tips for customizing the appearance of a single plot within a time series chart using FusionCharts or FusionTime

I've implemented a time series line graph using FusionCharts in the following code snippet: const MyChart = () => { const schema = [ { name: "Category", type: "string" }, { ...

What could be the reason for typescript not issuing a warning regarding the return type in this specific function?

For instance, there is an onClick event handler attached to a <div> element. The handler function is supposed to return a value of type React.MouseEventHandler<HTMLDivElement> | undefined. Surprisingly, even if I return a boolean value of fal ...

Exploring Vue.js lifecycle events and when to begin loading store properties (Vue.observable)

Currently, I am utilizing Vue.observable() for state management and it is crucial for two store properties to be fully loaded before most views are rendered by vue-router. I have attempted implementing the loading logic in various lifecycle events such as ...

How can I restrict the navigation buttons in FullCalendar to only allow viewing the current month and the next one?

I recently downloaded the FullCalendar plugin from Is there a way to disable the previous button so that only the current month is visible? Also, how can I limit the next button so that only one upcoming month is shown? In my header, I included this code ...

What steps should be taken to properly assess an AngularJS provider setup?

My provider definition looks like this: (function(angular) { angular.module('myModule', []) .provider('myService', function () { var service = {}; service.configureSomething = function () { }; service.$get = function () { ...

The "initialized" event in angular2-tree-component fires prior to the data being loaded

Utilizing the angular2-tree-component, my goal is to display an already expanded tree. According to Angular docs, the initialized event should be used for expanding the tree after the data has been received: This event triggers after the tree model has ...

Maintain a consistent header height with CSS even when using the calc() function

--dis: calc(var(--max-height) - var(--min-height)); /* @media large{--min-height:13rem}; @media small{--min-height:6.5rem}; --max-height:75vh; */ --percent: calc(var(--scroll-ani) / var(--dis)); /* in js: document.body.style = "--scroll-ani: ...

Trying to retrieve JSON data from an API in VueJS and successfully logging the results, but struggling to render the data on the page. (I

Having recently transitioned from React to VueJs, I encountered a problem where fetching data using axios.get returns a successful response in the console.log. However, when trying to iterate through the array with v-for, nothing is rendered. If you have a ...

Using Typescript to create an asynchronous function without explicitly declaring a Promise

When you examine TypeScript's async function, you may notice the redundancy with "async" and "Promise<type>". public async test(): Promise<string> { return "Test"; } Is there a way to configure TypeScript to handle async types ...