Fluctuating appearance during update - Vue Component

I have developed a component that searches for posts from an API based on the query from the URL. However, whenever I change the query, I encounter a strange flickering effect. The number of flicks increases by one with each change. How can I resolve this issue? Vue Devtools indicates that loadPosts() is being called multiple times: 1 time, 2 times, 3 times, and so forth. Where could the mistake be? Even reloading the component does not eliminate the issue.

<template>
    <v-app>
        <main-header/>
        <v-layout class="mx-auto default--container">
            <v-flex xs12 ma-2>
                <h2 class="display-2 text-xs-center main-page--header">
                    <span class="text__red">W</span>yniki wyszukiwania dla:
                    <span class="text__red">{{this.$route.query.s}}</span>
                </h2>
                <article-list-sample v-for="i in articles.length" :key="`${i}`" />
            </v-flex>
        </v-layout>
        <main-footer />
    </v-app>
</template>

<script>
    import MainHeader from './MainHeader';
    import MainFooter from './MainFooter';
    import ArticleListSample from './ArticleListSample';
    import API from '../api';
    export default {
        components: {
            ArticleListSample,
            MainFooter,
            MainHeader
        },
        name: 'search',
        data: () => ({
            articles: []
        }),
        methods: {
            loadPosts() {
                API.get(`posts?search=${this.$route.query.s}`)
                    .then(response => this.articles = response['data'])
            }
        },
        mounted() {
            this.loadPosts();
        },
        updated() {
            this.loadPosts();
        }
    };
</script>

<style scoped>

</style>

Answer №1

Additionally, the never-ending loop originated from the fact that the 'updated' function was being called every time 'this.articles' was modified, leading to a continuous cycle of asynchronous calls.

Answer №2

Success! I found the solution by incorporating a watcher instead of relying on the updated() method.

watch: {
            '$route' () {
                this.loadPosts();
            }
        },

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

While attempting to populate an array with data, I encountered the issue of receiving 'undefined' values within the

https://i.sstatic.net/YB5kX.pngIs there anyone who can assist me in resolving this challenge? I've been grappling with this problem for the past three days, and using jquery Ajax is a new experience for me. My goal is to dynamically populate a calend ...

In the n-th click event, the key press button is fired n times

I am working on developing a game that includes a start button. Once this button is clicked, the game will begin and involves various keyboard key press events. The issue arises when the start button is clicked multiple times causing the game to run repeat ...

Starting the setInterval function in JavaScript after it has been cleared

My setInterval function runs every 5 seconds and works fine on page load. I have encountered the following scenarios: Loading the page with interval (WORKS) Pressing a button to load new content and stopping the interval (WORKS) Loading new content, dis ...

Why won't my loop with THREE.ImageUtils.loadTexture function load the textures properly?

I've implemented code from three.js examples successfully in a different location, but it's not working within a loop. There seems to be something I'm overlooking as this is my third attempt at the code with no success. It appears that th ...

Creating dynamic popovers using vue.js and bootstrap 4

I'm looking to integrate popovers from Bootstrap 4 into my Vue.js app. While I could use something like https://www.npmjs.com/package/vue-popperjs, I prefer to explore how to achieve this without additional libraries in order to understand the underly ...

What are some ways to access my AsyncStorage data from any location?

I am utilizing the saga library and storing tokens in AsyncStorage. My goal is to be able to freely access the token retrieved from AsyncStorage in both the loadUserPosts function and the loadPosts function. Where should I add async and how can I correct ...

Checking if a Vector3 is visible within a camera's field of view using three.js

Is there a simple and cost-effective way to determine if a point or Vector3 is within the field of view of a camera using three.js? I would like to create a grid of boxes to cover the "floor" of a scene, but only up to the edges of the visible area, witho ...

Select a Node package for a TCP client

Looking to utilize a node package for establishing a TCP client. Have done some searching on npm already. Any recommendations for which package would be best? No need to set up a server, just looking to write to a server with my client. ...

Establish Angular data for all fields in the form

Being a beginner in Angular, I'm struggling with creating a form to update user information. Here's a snippet of my controller: // Fetch organization data from the database dataService.allOrganization().then(function ...

Using preventDefault before AJAX call, form submission still succeeds

I'm having trouble creating a form that requires specific conditions to be met before it can be submitted. I am facing challenges with ajax implementation... $('form').submit(function(event){ form = $(this); // I've also tried usi ...

Are there any JavaScript charting tools that can draw in a clockwise direction with multiple data points?

Can anyone recommend a JavaScript live chart/graph library that can create clockwise graphs with multiple points, similar to the example below? https://i.sstatic.net/dQfK4.png ...

Python script used to extract data from Vine platform

I am looking to extract post data, such as title, likes, shares, and content, from various brands' public accounts on Vine using Python. Currently, I have a few ideas in mind: There is a Vine API called Vinepy available on GitHub (https://github.c ...

Issue with updating dropdown values in real-time in React

I am a beginner with React and I have a question regarding fetching dropdown values from the backend. Despite using async-await functions, the list is not getting populated with items. Any assistance in this matter would be greatly appreciated. Here is th ...

Why does my Three.js Object look Empty when Clipped?

Currently experimenting with clipping portions of a box in three.js, however, encountering an issue where the inside appears hollow when the box crosses the ClipPath. Any suggestions on how to render the box so it remains solid after being clipped? Alter ...

JSDoc encounters issues when processing *.js files that are produced from *.ts files

I am currently working on creating documentation for a straightforward typescript class: export class Address { /** * @param street { string } - excluding building number * @param city { string } - abbreviations like "LA" are acceptable ...

Converting an array containing objects from JavaScript to PHP for database insertion

Hey, I'm dealing with an array that contains objects, and it's structured like this: https://i.sstatic.net/48gMT.png I want to insert this data into my database using an AJAX call to my PHP file. I've attempted to use JSON.stringify on th ...

Problem arising post jQuery update from version 1.11.1 to 3.3.1 - Popup malfunction

I had been using jQuery 1.11.1 and jQuery Mobile 1.4.5 without any issues. However, after upgrading to jQuery 3.3.1, I encountered a problem with popups. The following error is displayed when attempting to open a popup by clicking a button that calls the p ...

The corner of cubes in Three.js is unfortunately not visible

After setting up an Orbital Controls with a boxGeometry to display a box, I encountered an issue where the corners of the box were not being rendered properly when zoomed in. My current setup involves using react-three-fiber and react-three-drei const Sce ...

Proper date formatting in Angular Data Table

Could anyone help me with date formatting? I am facing an issue with the date format in a table within my application. The current date format is not correct and appears as shown on the screen. I would like to format it to YYYY-MM-DD only. Any suggestions ...

The Laravel Vue page is failing to load and appears to be stuck in the network tab

I have a question regarding Laravel and VueJS. I developed my Vue project using the command npm run watch and launched my Laravel application with php artisan serve to host the app. However, when I tried to access my website page, it kept loading indefinit ...