Is there a recommended approach in Vuex to detect when a state change has occurred?

Our team has developed a Vuex module to store user-input data from a form. If the form has unsaved changes and the user tries to leave the page, we want to warn them so they can decide whether or not to proceed.

It would be convenient if we could simply use a built-in getter to check for changes. Something like this:

if (this.$store.getters['myModule/hasChanged']) {
    // alert user
} else {
    // allow user to continue
}

We have discussed watching the state closely and creating a hash every time there is a mutation, but we are concerned about the potential performance issues that may arise from that approach.

We have also thought about using a boolean flag to indicate whether changes have occurred, either in the component holding the form or within the state itself. However, we prefer not to hard-code such a value.

Is there a standard practice for determining if the state of a specific module has changed?

Thank you.

Answer №1

Here's a different suggestion:

  • Keep all inputs within the component
  • Monitor changes within the component and set a boolean flag called isModified
  • When the user tries to leave:
if (this.isModified) {
    // notify the user
} else {
    // save all inputs in a state
    // let the user continue
}

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

Axios saves the same data for each loop iteration

I am facing an issue where I need to store an array of objects in a database when a single button is clicked. I have tried using a foreach loop and inside the loop, I used AXIOS POST to save the information. However, AXIOS only stores the same object repea ...

Tips for adjusting the autocomplete maxitem dynamically

I am working on a multi autocomplete feature and I have the following code. Is there a way to dynamically set the maximum number of items based on the value entered in another text field? <script> $(function () { var url2 = "<?php echo SI ...

Can we convert a PHP frontend into a Vue node?

I'm currently working on transitioning a legacy PHP application to Vue, and I want to do it gradually. The current PHP app is quite messy, with HTML and JavaScript files intertwined in a complicated manner like this: foo.js.php ... <script src=&quo ...

Activate the parent navigation link when on sub-pages

I want to ensure that the parent navigation item is highlighted when a user is on a child page based on the URL. For example, if the user is currently viewing foo1-child and the parent is foo1, I need to apply the active class to Foo 1: http://foo.com/foo ...

To manipulate the array in Vue by adding and removing selected HTML elements

Sharing my code snippet: <form> <div v-for="(inputPlane, index) in inputsPlanes" :key="inputPlane.id" :id="`plane-${index}`"> <input placeholder="origin" name="data" /> < ...

Navigate to the anchor element within the webpage that contains adaptive images

My Bootstrap 4 page contains responsive images and anchor tags within the text. .img-fluid { max-width: 100%; height: auto; } Once I navigate to this page by clicking a link (e.g., 'mypage#section-one'), the page initially loads on the ...

I'm attempting to create a button using html, but I'm puzzled as to why it's not functioning as expected

I've been working on creating a button that, when pressed, generates a new div string based on the node.innerHTML code. For some reason, my code doesn't seem to be functioning properly and I'm not sure why. Here's the HTML: <input ...

"Unleashing the Power of AngularJS: Implementing a Global Error Handler to Display and

When building my website, I have multiple Angular apps that require a global error handler to track and display alerts for errors with codes like 500 and 401. Here is what I have so far: In order to create a global error handler module, I've set up t ...

Ways to acquire a reference to a dynamically generated child element with an unknown name in JSON

JSON data is structured as follows: "json": { "schema": { "type": "object", "title": "My Table", "properties": { "table-1659555454697": { ...

Node.js PDFKit Measurement Units

Can you clarify the measurement unit used in PDFKit (Node.js)? For example, we have: doc.text(20, 20, 'Message') What exactly does 20(x) and 20(x) represent? Are they measured in centimeters, millimeters, inches, or something else? Can I conver ...

Is there a way to ensure an ajax call finishes executing without relying on 'async: false' or callbacks?

In my view, I have implemented a TypeScript code defining a KnockoutJS binding handler for a clickable element as shown below: module MyModule { export interface ICopyButtonParams { dataUrl: string; } ko.bindingHandlers.copyButton = { ...

IE8 and IE9 encountering "Access is denied" error due to XML causing XDomainRequest (CORS) issue

Sorry if this seems repetitive, but I am unable to find a definitive answer to similar questions. Whenever I attempt to make a CORS request for XML, I consistently encounter an "Access is denied" JavaScript error in IE8. The code I am using is based on t ...

What is the best way to set up a Vue bus to handle global events?

I am working on a Vue project within a Node/Webpack/Vue Router environment and attempting to set up a global event handler or bus. However, I am running into an issue where it is showing as undefined. Below is how I have set it up: main.js: //Defining th ...

What is the correct way to observe an item in Vue?

I am currently experimenting with utilizing a Vue watcher on a computed object, however I'm encountering issues. Interestingly, the watcher functions as expected when dealing with a string, but not with an object. Despite following the guidance provid ...

Challenge with the Nested List Weight Sum algorithm

Trying to crack the challenge "Nested List Weight Sum": Challenge: If given the list [[1,1],2,[1,1]], the expected output is 10. (four 1's at depth 2, one 2 at depth 1) Here's my approach. function calculateDepthSum(nestedList, sum=0, dept ...

Tailwind component library - personalize color schemes for your project's design

I recently created a component library using Tailwind and React, and I've utilized Rollup for building the distribution files. In my tailwind.config.js file, you'll find the following color declarations: const colors = require('tailwindcss/c ...

Encountering a display:flex problem with postcss in Vue CLI 3

Issue with Vue CLI 3 Configuration In my current setup, utilizing vue cli 3 with all default configurations including autoprefixer and postcss, I have encountered a challenge. The problem arises when using Samsung internet version greater than 7, as the ...

Unchecked checkbox displays as checked in UI

I am facing an issue with checking checkboxes using JavaScript. Even though the checkbox appears to be checked in the program, it does not reflect the updates on the user interface. If anyone has a solution for this, please share. <!DOCTYPE html> ...

Conceal specific select menu choices in VueJS based on a specified condition

I am working on a project that involves 3 select drop-down menus. The goal is to dynamically hide specific options in the drop-down menus based on the selections made in the other 2 menus. For example: If I choose 'English' as the subject in the ...

Angular 2 rc4's HTTP requests are generating uncaught exceptions (in promise)

Is there a change in Angular 2 HTTP service after upgrading to Angular 2 rc4? I have spent the entire day going through Angular HTTP client documentation and searching on Stack Overflow, but I can't seem to find the solution to my problem. Here are ...