The state of a Vue.js component is being lost after an event is triggered

I have encountered a specific issue. Within my Vue component, I have set up an event trigger for when an item is added or removed from a list.

  export default class HelloWorld extends Vue {
  @Prop() private msg!: string;
  @Prop() private idCount: number = 0;
  @Prop() private toDos: ToDo[] = new Array<ToDo>();
  @Prop() private toDoText: ToDoText = new ToDoText();

  public NewToDo() {
    let element = document.getElementById("NewToDoName") as HTMLInputElement;
    let name = element.value;
    element.value = "";

    var toDo = new ToDo();
    toDo.name = name;
    toDo.id = ++this.idCount;

    this.toDos.push(toDo);
    this.$emit('scrollChange');
  }

  public DeleteToDo(index: number) {
    this.toDos.splice(index, 1);
    this.$emit('scrollChange');
  }
}

In response to this event within the parent component:

<HelloWorld msg="Welcome to your ToDo-App" v-on:scrollChange="onChanged()" class="jumbotron"/>

This is the method implemented:

    onChanged(){
    this.canScroll = true;
    return true; 
  }

The property canScroll is linked to another child component:

<NavBarBottom v-bind:canScroll="canScroll"/>

Containing the following logic:

<nav class="navbar navbar-expand-lg navbar-dark bg-dark" v-bind:class="[!isFixed ? 'fixed-bottom' : 'bottom']">

export default class NavBarBottom extends Vue {

@Prop() public canScroll : Boolean = false;
@Prop() public isFixed : Boolean = false;

@Watch('canScroll')
onChange(){
    //this.isFixed = this.hasVerticalScroll(undefined);
    console.log(this.isFixed);
}

private hasVerticalScroll(node : any){
    //some checks
}
}

Initially, everything functions properly upon firing the event.

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

However, upon adding another item to the array in the HelloWorld-Component, nothing occurs. Upon inspecting the debugger, it appears that the component state has been reset like so:

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

Could someone provide insight into why this behavior is happening?

Thank you for any assistance!

Answer №1

In looking at the example using @Prop (vue-property-decorator), I believe that adjusting how the prop is initialized may resolve this issue. Please consider using default: x instead of explicitly defining the initial value. Here is how your code could be updated:

@Prop() private msg!: string;
@Prop({ default: 0 }) private idCount: number;
@Prop({ default: new Array<ToDo>() }) private toDos: ToDo[];
@Prop({ default: new ToDoText() }) private toDoText: ToDoText;

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

Navigating through Ember applications using the root URL

Although I have primarily focused on back-end development, I recently ventured into the world of Ember front-end framework. I encountered a scenario where I needed to serve two Ember apps from the same domain. To achieve this, I followed the steps outline ...

Creating an array in TypeScript and determining its type as an object

Currently, I am in the process of creating an app with Ionic, utilizing TypeScript as the primary language. When declaring an array in my code: products = new Array(); I expect it to be recognized as an array. However, when I check the type using consol ...

Encountering a 404 error while sending a session ID in a Post request using AngularJS

My services are hosted on a remote server, and I am consuming these services in a local AngularJS app. Everything works fine with REST requests that do not require a SessionID in the header. However, when I add the Session ID in the header, it does not wor ...

The function res.sendFile() does not display files on the browser

For the past few days, I've been facing a challenge. Does anyone have any suggestions? When I click on a login button, I authenticate the user, generate a token, store it in cookies, and then use it in the headers of a request to display the homepage. ...

Is there a way to make the ExecuteQueryAsync method behave in a more synchronous manner?

Okay, so I'm using JavaScript in SharePoint and relying on Alerts to debug my script that runs from a Content Editor Web Part located in the Assets library. I understand that "async" function calls are meant to not wait around for the call to finish, ...

The Jquery onclick function is executing multiple times, with each iteration increasing in

I have encountered an interesting problem that I can't seem to resolve. The issue involves dataTables and the data that is retrieved via jQuery ajax post after a selection change on a select element. Furthermore, I have an onclick function for multipl ...

What is the process through which Typescript determines the property types of union types?

I am implementing a unique set of types to enable angular form typing: import { FormArray, FormControl, FormGroup } from '@angular/forms'; export type DeepFormArray<T, U extends boolean | undefined = undefined> = T extends Array<any> ...

Having trouble printing a section of a webpage after making CSS adjustments

When trying to print part of a page, I used the following method. It successfully prints that portion of the page, however, it does not preserve the CSS effects. <body> <h1><b><center>This is a test page for printing</center&g ...

The scrolling behavior varies in Firefox when using the mouse wheel

Currently, I am working on a website where I want to display large text on the screen and have the ability to scroll two divs simultaneously. This functionality is already in place. I can scroll the divs, but I'm facing an issue with the jumps being t ...

Utilizing a Static Image Path Prop in React JS: A Step-by-Step Guide

My Main Component import React from "react"; import TopModal from "./components/topModal"; // Passing productImage to the Child Component import productImage from "../../../../../../assets/images/juices.jpg"; import { makeS ...

Saving variable with the selected option from the DropDown menu, and subsequently utilizing this value to display a specific HTML page

I have a dropdown menu that pulls values from my SQL database. Once a value is selected, it is stored in a variable. For example, if I choose "ABC" and store it in the variable, I want to use this variable to open a local HTML file named ABC.html. The o ...

Leverage object properties as data table field values in VueJS

In my current project, I am utilizing VueJS (version 2.5.9) to display and modify data tables within an administrative application. Initially, I used a basic Grid component for the data table, following an example provided here. However, I came across an e ...

Creating a jQuery thousands separator without losing the decimal accuracy with .toFixed(2)

I'm currently working on a Wordpress project where I need to create a pricing table using jQuery. One of the specific requirements is to format the numbers in thousands with spaces, like converting 1000 to 1 000. Although I have attempted various jQu ...

Having difficulty troubleshooting the /app router application on version 13.4.x

Having trouble debugging a server-side process in my Next.js app that uses the /app router. To reproduce the issue, simply create a new Next.js app with npx create-next-app and select the app router option. I've attempted to attach a debugger to the ...

DataTables: Display action button based on specified condition in query

I am using jquery DataTables to display data via an ajax call. Everything is loading smoothly and I have included an action button in the last column that will add the data to another database. However, I am struggling with one aspect: After the button ...

Utilizing Angular 4 alongside ng-sidebar to incorporate the class "right"

Just started using ng-sidebar in my Angular 4 project, but I'm a bit lost on where to place the "ng-sidebar--right" class. Could someone please guide me through this small issue (I'm new to this, so apologies in advance). Here's a snippet of ...

What are the reasons for JSON being considered type-safe?

Recently, I came across a statement suggesting that JSON is type-safe. I understand that vanilla JavaScript is not considered type-safe. However, how can a data interchange format like JSON be classified as type-safe? ...

What is the best way to search for a specific item in Express.js?

I recently got the Leaderboard API issue fixed, but now I'm encountering a new problem with express Querying. Specifically, I'm attempting to search for a specific Discord ID using quick.db as my database. I've included an excerpt of my expr ...

Creating dynamic text bubble to accommodate wrapped text in React using Material-UI (MUI)

I am currently developing a chat application using ReactJS/MUI, and I have encountered an issue with the sizing of the text bubbles. The bubble itself is implemented as a Typography component: <Typography variant="body1" sx={bubbleStyle}> ...

Electron fails to display images in the compiled version

I'm currently troubleshooting an issue related to displaying images using CSS in my electron project. In the latest version of the application, the images are not showing up when linked from a CSS file. However, in a previous version, the images disp ...