The Vue code shows reactivity in one sandbox but not the other

Recently, I encountered an issue while trying to create a minimal reproducible sandbox for a coding problem. After forking my previous sandbox and removing irrelevant code, one particular feature suddenly stopped working in the new version. Despite checking that the code was updated and comparing it to the original sandbox where it worked fine, I couldn't figure out why there was a difference in behavior between the two.

The original sandbox can be accessed here: https://codesandbox.io/s/frosty-taussig-v8u4b. In this version, clicking on buttons with numbers increments them instantly as expected.

However, in the minimized sandbox available at: https://codesandbox.io/s/nervous-breeze-ejznz, clicking on these same numbered buttons does not trigger any action until the screen is refreshed.

This discrepancy perplexes me, as the source code remains consistent across both versions of the sandbox. I am keen to understand why the functionality varies despite having identical codebases.

Here are snippets from the mutation and action causing the issue:

SET_VOTE: (state, payload) => {
  console.log("SET_VOTE");
  const { commentId, vote } = payload;
  const comment = state.comments[commentId];
  if (vote === 1) {
    comment.up += 1;
  } else {
    comment.down += 1;
  }
  console.log(comment);
}

Action:

COMMENT_VOTE: async (context, payload) => {
  console.log("COMMENT_VOTE", payload);
  const mutation = {
    commentId: payload.commentId,
    vote: payload.vote
  };
  context.commit("SET_VOTE", mutation);
}

Comment.vue snippet:

<b-button v-on:click="upvote" class="mr-1">
  {{ comment.up }}
</b-button>

async upvote() {
  await this.$store.dispatch("COMMENT_VOTE", {
    vote: 1,
    commentId: this.comment._id
  });
},

https://i.sstatic.net/TNUBm.png

Answer №1

Once again, you overlooked adding reactivity. Remember to utilize Vue.set for assigning a new property in the comments state:

  Vue.set(state.comments, comment._id, comment);

rather than

  state.comments[comment._id] = comment;

Answer №2

Success at last! The secret sauce was:

data() {
  return {
    userFeedback: this.feedback.received,
  };
computed: {
  thumbsUp() {
    return this.userFeedback.filter(feedback => feedback.type === 'positive');
  },
  thumbsDown() {
    return this.userFeedback.filter(feedback => feedback.type === 'negative');
  },

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 is the process for transforming this information into JSON format with Javascript?

I have a server response with data that needs to be converted to JSON using JavaScript. country=Philippines\r\n countryid=840\r\n operator=Globe Telecom Philippines\r\n operatorid=246\r\n connection_status=100\ ...

The concept of global object/scope and circular references in undefined cases

Trying to make sense of the outcomes from these few experiments : Experiment number 1 (in a new command line) : > _ ReferenceError: _ is not defined at repl:1:2 at REPLServer.self.eval (repl.js:110:21) at Interface.<anonymous> (repl. ...

What is the best way to link the data from the HTML input to the class property in the TypeScript file? (Combining Angular and IntroJs)

I'm working on an onboarding screen with Intro.js and need help with receiving user input. I've been trying different methods like [(ngModel)] = checked, [(checked)] = checked, (checked) = checked, but haven't had any success. Can anyone pro ...

Utilizing Google Sheets as a secure, read-only database for Angular applications without the need to make the sheet accessible to the

Seeking a way to utilize Google Sheets document as a read-only database for my Angular application, I have attempted various methods. However, the challenge with all these approaches is that they necessitate public sharing of the Sheet (accessible to anyon ...

Change the URL structure from ex.com/forum?id=1 to ex.com/#/forum?id=1 in AngularJS

Hey there! I'm in the process of creating a Forum using AngularJS and need some guidance. First things first! I've successfully established a connection to my database with: <?php session_start(); $db = new mysqli("localhost","root",""," ...

The appearance of a slide-in hamburger menu causes the horizontal scrollbar to appear

To create a hamburger menu that slides in from the right when clicking the icon, follow this code snippet. Here is the main menu code where it is initially translated to the right by 100% and on icon click, it comes back on screen with a translation of 0% ...

What is the best way to display a link after multiple selection steps based on chosen options?

I am looking to create an order page that is based on user selections. I offer 4 variations of a product, and the user must first choose their country and then select the product type. This process should be broken down into steps: Step 1: User selects Co ...

JavaScript Function for Finding the Time Difference Between Two Dates (in Years, Days, Hours, or Less than One Hour)

I need to calculate the time difference between 2 dates and display it. If the difference is greater than a year, show the number of years only. If it's more than a day, show the number of days. If it's less than a day, show the number of hours. ...

How to attach functions to nested elements in an AngularJS directive

Being a newcomer to angularjs, I am facing difficulty in resolving this issue. Custom Directive app.directive('rating', [function () { return { restrict: 'E', scope: { maxStars: '=', }, link: funct ...

Utilizing module.exports functionality within Express.js

Recently, I was working on a Discord bot using discord js and I wanted to integrate some frontend aspects to allow for changes through a website interface rather than just commands. However, I ran into an issue with utilizing module.exports in expressjs. I ...

Unable to retrieve basic profile data from LinkedIn Members using their email ID unless they are signed in

I am struggling to retrieve the basic profile details of Linkedin Members using their email ID. Despite my efforts, I haven't been able to find relevant information in the documentation. My attempt involved creating an app, initializing the JavaScrip ...

What is the process for animating the rotation of my FBX model using the animate function?

I'm trying to figure out how to create a continuous rotation for my FBX model using three.js. Here's what I've attempted so far: Declared my object as a variable Called my object in the animate function for rotation (girl1) However, I enc ...

Updating or swapping images using JavaScript and HTML

I am looking to implement a feature similar to Twitter, where uploading a picture automatically updates the avatar by displaying a spinner while the new image loads. I attempted to accomplish this with the following code: <script language="javascript"& ...

Tips for importing a JavaScript file from a URL and initializing a class in a React component

I am looking to incorporate the PitchPrint app into a React website. They offer a tutorial for vanilla HTML/JS integration here. Following their instructions, I included script tags with links to jQuery and their app file in my index.html file. I then crea ...

retrieve PHP function calls as an array using Ajax

While working in PHP, I have encountered a situation where I needed to call a PHP function using AJAX: <button onclick="loop()">Do It</button> function loop() { $.get("ajax.php", { action: "true" }, function(result) { $("in ...

Does vee validate only apply to the initial field?

I've gone ahead and created two modals, one for logging in (loginModal) and another for registering (registerModal). Users have the ability to seamlessly switch between the two. However, I've encountered an issue with the "VeeValidate" plugin. I ...

Utilizing Angular to link HTTP response data with a subsequent HTTP request

Looking for assistance with handling HTTP calls in Angular: public getCommuneByCode(code: string): Observable<Commune> { return this.http.get<Commune[]>(ApiUrl); } The Commune model structure: export interface Commune { code: string; d ...

I am unable to retrieve information from Firebase in Vue

Currently, I am following a tutorial on YouTube to enhance my knowledge about storing data in Firebase and then utilizing it in a Vue application. (You can watch the tutorial at this link: https://www.youtube.com/watch?v=Htt8AKeF1Kw, you will encounter the ...

Difficulty in displaying additional search outcomes

I decided to take on the challenge of learning coding by working on a website project that involves creating a simple search site. One feature I implemented is when users search for keywords like "Restaurant" or "Restaurants," they are presented with refi ...

In the strict mode tree, a reference named "grid" has been discovered

I encountered the following warning in the console: Warning: A string ref, "grid", has been found within a strict mode tree. String refs can potentially lead to bugs and should be avoided. It is recommended to use useRef() or createRef() instead. T ...