What is the alternative for watchEffect in VueJS 2?

Once my VueJS 2 component loads, I fill a props variable with data. Here's how it's done:

  created() {
    this.events = null
    Service.getEvents()
      .then(response => {
        this.events = response.data
      })
      .catch(error => {
        console.log(error.response)
      })
  }
}

Now, I'm looking to refresh the component with the "events" props when the user clicks on a next page link. I came across a VueJS 3 tutorial that demonstrates how to achieve this using the watchEffect method (https://v3.vuejs.org/guide/reactivity-computed-watchers.html#watcheffect)

Does anyone know how I can achieve similar functionality in VueJS 2?

I attempted to use the watch() method on the "events" variable, but it leads to an infinite recursion due to the changes being made to the "events" object inside the method.

//Error - recusrsion
watch: {
    events() {
    this.events = null
    Service.getEvents()
    .then(response => {
              console.log(response.data)
              this.events = response.data
            })
    .catch(error => {
              console.log(error.response)
            })
        }
    },

Any idea on how I can reload the events on the same page when the user interacts with a button or link?

Answer №1

Within your code, the reference to this.events signifies the watch variable...

This ultimately leads to recursion.

If you wish to include an event for clicking on the next page,

You can simply add the @click= event props to the component of the next page props.

Answer №2

When utilizing the options API, the watch method is able to access both the previous and current values:

watch: {
    event: function (oldEventData, newEventData) {
        console.log(newEventData) // handle the new event data accordingly
    }
},

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

Unable to launch the Express application on a 64-bit Windows 7 operating system

After successfully installing Node for Windows/64bit and being able to run the server with an example from the Node homepage, I encountered an issue when setting up an Express app. Despite installing Express using the command npm install -g express and r ...

Interactive window allowing the user to closely examine code

Hey guys, I need your help with something Is there a way (PHP / jQuery) that allows me to zoom in on my code? Similar to how lightbox works for images. I specifically want to zoom in on dynamic code while using Yii's CListView. I'm thinking of ...

Can you create a dynamic visual display using HTML5 Canvas to draw lines in a circular pattern that react

I have successfully implemented drawing lines around a circle using the AudioContext API. However, I am facing an issue with the lineTo function, as the line only grows and does not shrink. My inspiration for this project comes from the audio visualizer fo ...

How to invoke a function in an isolated scope of AngularJS using JavaScript

We are in the process of gradually switching our application to work with Angular. I have developed an angular module and a directive that aims to connect specific functionality to an HTML element. angular.module('myApp', [ ]) .controller(&ap ...

What is the process for implementing a grid with 5 columns on larger screens and 2 columns on smaller screens using reactjs?

I am currently working on building a Grid using material UI and reactJs. I found the guidelines on this link https://mui.com/system/react-grid/. However, there seems to be an issue with creating 5 columns in the grid. I attempted to create a custom grid ...

Navigating in Vue.js to a default page when a route cannot be found

This is my basic routes file for Vue.js. import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); // Import components import home from './components/home.vue'; import about from './components/abo ...

When using Vue and Axios to make a GET request to the YouTube search API, I received a 404 error

Having some trouble with my code. I'm attempting to send a get request to the YouTube API using a specific keyword, but the query string is returning a 404 error even though I've included referring URLs and have a valid API key. The method where ...

Utilizing Typescript Generics in Arrow Function to Combine Two Arguments

For instance, I am working with this code in a .tsx file extension const Add = <T,>(arg0: T, arg1: T): T => arg0 + arg1; const A = Add(1, 2); const B = Add('1', '2') However, I am encountering an issue, as there is an error m ...

Is it possible for Webpack's DefinePlugin.runtimeValue to access the module's entry point?

https://i.sstatic.net/D0XMT.png Similar to this, I am looking to utilize the DefinePlugin in order to define a global variable ECZN for my application using the Webpack runtimeValue. Additionally, the WebpackModuleInfo is retrieved from the file where I ...

How to retrieve the length of an array stored in the data object of a Vue instance

Having trouble retrieving the length of an array in vue. The array is located in the data object like so: data() { return { slides: [ { image: require("@/assets/img/carousel/couple.jpg"), caption: "A coupl ...

The Meteor update is unsuccessful on the Mongo Sub Collection and will not continue

I am currently facing an issue with updating a specific object within an array in my object based on a value. Whenever I try to add the update code, the function gets stuck at that point without proceeding further. None of the console.log calls after the u ...

What is the proper method for invoking object (class) methods from a router?

My apologies for the vague title. Let me clarify what I am attempting to accomplish. In this scenario, there are two main components: A class called 'wallet.js' A router named 'index.js' which handles GET requests This is my objectiv ...

The operation to remove the child element could not be completed

var first_content = document.getElementById('first-content'), offered_games = document.getElementById('offered-games'); for(var i = 0, e = offered_games.children; i < e.length; i++) { e[i].onmouseenter = function() { var ...

Is there a way for me to monitor a prop using the composition api?

Is there a way to trigger a function upon prop update? The parent container structure is as follows: <div> <Maintable :type="typeRef" :country="countryRef" /> </div> And here's the child container code: e ...

What steps can I take to ensure that my logos remain visible even when I close the menu and resize the window?

On my website, I have a menu of logos that are clickable. These logos always display except on smaller screens, where they need to be toggled to show using a hamburger menu. The menu toggles fine when it is on a smaller screen, but there is an issue when y ...

Learn the process of adjusting opacity for a specific color in CSS

At the moment, this is the code I'm using to apply a color to an element using jss. const styleSheet = theme => ({ root: { backgroundColor: theme.colors.red, }, }) I am interested in finding out if there is a way to add opacity based o ...

Rules for specifying title attribute for tag a in JavaScript

What is the best way to handle conditions for the a tag (links) when it has content in the title attribute? If there is no description available, how should the script be used? For instance: if ( $('a').attr('title') == 'on any ...

Is there a way to retrieve the input element's value within the controller using ng-if?

After clicking on the submit button, I want the ng-if condition to activate in the DOM so that I can access the properties of the input element or apply a class to it. <div ng-app="myModule"> <div ng-controller="myController"> <div ng-if ...

The computer system encountered an issue in computing the values of the text

Possible Duplicate: Unable to get textfield value using jQuery My current project involves updating input fields based on user changes in quantity for items. Each item is retrieved from a database and I am generating invoices for customers. However, ...

CloudFront for Amazon - Limit MP3 playback to designated website

I've been trying to find a solution for allowing mp3 files in an Amazon S3 bucket paired with Cloudfront to be streamed directly on my site without revealing the source URL of the mp3s. I want to prevent others from sharing or leeching the link by vie ...