Avoid repeated rendering in Vue v-for loops

Here is the code I've captured: https://jsfiddle.net/prsauer/Lnhu2avp/71

The main issue at hand is that currently, every click on a list item triggers a call to computeStyle for each item. I would like it so that only one recompute of the style occurs per click.

<div id="editor">
  <div v-for="item in dungeons" :key="item.isOpened">
    <div v-on:click="clickedChest(item)" v-bind:style="computeChestStyle(item)">
      {{ item.name }} {{ item.isOpened }}
    </div>
  </div>
</div>

var dgnData = [
    { name: "Lobby", isOpened: false },
  { name: "Side", isOpened: false },
];

new Vue({
  el: '#editor',
  data: { dungeons: dgnData },
  computed: { },
  methods: {
    clickedChest: chest => {
      chest.isOpened = !chest.isOpened;
      console.log("##### Clicked chest", chest);
    },
    computeChestStyle:
        item => {
        console.log("computeStyle", item);
        return item.isOpened ? "color: red" : "color: blue";
        }
  }
});

Answer №1

element, it is highlighted that function calls are re-evaluated each time the view is updated. To optimize performance and ensure results are cached for efficient rendering only when necessary, utilizing a computed property is recommended. This involves creating a component for the specific item and defining the computed method within the component itself.

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

Updating data dynamically in pug front end using Axios call response

I am currently working on implementing a search form along with an endpoint to retrieve the results. Once I receive the response, I want to dynamically add a div element to my pug/jade file to notify the user whether an order has been found or not. Here i ...

Save geometric shapes data in the SQLite database

How do I go about storing polygon data in an SQLite database? Important: I am utilizing the Cordova plugin. polygon: Point[]; interface Point { x: number; y: number; } https://i.sstatic.net/5EYf2.png ...

Using ValidationGroup to trigger JavaScript calls from controls

Is it possible to trigger a JavaScript function from the "onclientclick event" of a button that has a ValidationGroup assigned? <asp:Button ID="btnTest" runat="server" Text="Test" OnClick="btnTest_Click" ValidationGroup="Valid ...

Angular - Implementing selective binding in a function

In my current Angular project, I am utilizing Datatables and buttons to manipulate data. The buttons available are "View", "Mark as completed", and "Mark as incomplete" which trigger specific functions when clicked. To achieve this functionality, the follo ...

Looping through a collection of JSON objects without a parent element

Utilizing jQuery UI Autocomplete, I have encountered a situation where the documentation mentions that the source can consist of a list of JSON objects. The current code is functional; however, it lacks a root element for the list of JSON objects. <scri ...

The JQuery clone method is failing to generate a new table row

I have a table with a button that is supposed to add a new row dynamically when clicked. However, the functionality is not working as expected. The HTML and JQuery code can be found in this JSFiddle link. Any suggestions on how to fix this issue would be ...

What steps should I take to resolve a library add error within my Vue.js project?

I am encountering an issue while trying to install the Vuelidate vue library in my existing project. I have tried using the commands npm install --save vuelidate and npm i vuelidate, but I keep getting the following errors. Can someone please assist me? ...

Enhancing Angular with Plotly: Implementing click events on bar chart legends

I'm currently working on implementing color pickers for my plotly-plot charts within an Angular template. I am looking to add a function that triggers when the chart legend is clicked. How can I achieve this and get a click event for the chart legends ...

Unable to load page redirection

I am experiencing an issue with this page not redirecting to the appropriate mobile or desktop page when accessed. Below is the code snippet in question: <html> <head> <title>Loading...</title> </head> < ...

Any number of elements in a single JSX code snippet

In my React code, I've created a function called optionTemplate to be used as a prop in a React component. const optionTemplate = (option) => { return ( <div className="flex align-items-center gap-2" style={ ...

Looking for a comprehensive calculation that takes into account various input values?

I need to display the List Price at the bottom of the form so users are aware of the cost to list their item. Within a php file, I have defined price brackets. For example, if the listing price is £150.00, it falls between £100 and £199.99 and thus nee ...

What steps should I take to verify that the most recent version of a static website is being shown?

As a user, I often find myself hitting 'F5' or clearing my cache to ensure I'm seeing the most up-to-date content on dynamic websites. However, I'm curious if there are any techniques from a web designer's perspective that can aut ...

Please maintain the previous file attached if no changes have been made to the input file

Is there a way to prevent the selected file from disappearing when a user clicks on input['type="file"'], browses for files, but does not select one and closes the dialog? ...

Difficulty encountered when trying to access the vuex mutations within the store/index.js file

I humbly confess that I am drawing inspiration from SAVuegram to establish authentication for my app. I have implemented Firebase's onAuthStateChanged to ensure that the user remains logged in during page reloads, but I am encountering difficulties in ...

State updates in React Hooks are only applied once when they are called from callbacks within an array of components

I am facing an issue with updating the state in this particular scenario: I have a button that, on each click, adds elements to 'mylist' using 'setMyList'. These elements are 'Child' components (the div className "site" repr ...

Efficiently updating database records without the need for page reloads using EJS

I'm working on a project that resembles the comment section on Reddit. Users can leave comments and others can reply to those comments. My tech stack includes Node, Express, MySQL, and EJS. The issue I'm facing is implementing the upvote/downvo ...

What is the best way to update my JSX filter and add another condition to it?

I have a function that I use to filter and sort HTML items in a list. Sometimes, the filter results in zero items passing the check. In such cases, it would be ideal for a specific HTML element to appear: <span> no items pass the filter </span> ...

How can I redirect the page in CodeIgniter after successfully validating?

Currently, I am utilizing CodeIgniter for my project's development and implementing field validation using Bootstrap validator. Although the Bootstrap validator is functioning properly, I am encountering an issue. Upon successful validation, I expect ...

Is there a way to modify the state following a sorting operation?

How can I properly update the state after sorting by salary? state = { workers: [ {name: 'Bob', surname: 'Meljanski', salary: 5140}, {name: 'Michel', surname: 'Hensson', salary: 5420}, {n ...

Tips for: Minus a CSS property from a JavaScript variable height?

Is there a way to deduct the navbar_height by 2rem in the code snippet below? navbar_height = document.querySelector(".navbar").offsetHeight; document.body.style.paddingTop = (navbar_height - 2 ) + "px"; Appreciate your help! ...