Improving form handling with Vuex: Ensuring state is only updated after pressing the submit button

I am currently working on developing a form that pulls data from a Vuex store, allowing users to update the form fields and then submit when they are ready.

Most tutorials I have seen use v-model with computed get() and set() to retrieve values from the store and directly update them.

However, I would prefer to enable users to make changes to the form intuitively and then decide whether to update or cancel. Canceling should reset the form to its original state values.

Currently, I have only managed to implement the v-model method, but it is not ideal as it affects all instances where the state value is used, potentially impacting logic prematurely before submission.

<v-text-field v-model="displayName"></v-text-field>

...

computed: {
...mapGetters(['currentUser']),
    displayName: {
        get() {
            return this.currentUser.name
        },
        set(value) {
            this.$store.dispatch('updateName', value);
        },
    },
}

I am struggling to display the current state in a Vue-like manner while also updating the state only when a button is pressed. Are there any good tutorials available for this?

My idea involves updating a temporary state and then applying those updates to the actual state and database upon submission. What do you think of this approach?


I referenced this tutorial https://medium.com/js-dojo/maintaining-a-single-source-of-truth-while-handling-form-data-with-vuex-426e86d16642

Answer №1

In order to optimize performance, consider maintaining the form state locally as mentioned in your post. Upon component load, retrieve data from the store and store it locally. When the user saves their changes, dispatch an action to update the API and store.

Alternatively, you could follow the suggestion in your comment, but in this case, if the user decides to discard their edits, the original values will need to be reloaded from the server.

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

Include scrollView on smaller screens based on conditions

While incorporating an overlay in my application, how can I integrate a ScrollView specifically for smaller devices? Initially, I am checking the width: const windowWidth = Dimensions.get("window").width; Subsequently, I am attempting to display the Scro ...

Step-by-step guide for adding an icon to the corner of a Material UI button

Is there a way to position an icon in the corner of a Material UI button in React? Currently, I have the icon next to the title but I would like to move it to the lower right corner of the button. Any suggestions on how to achieve this? Thank you! export ...

Mobile Image Gallery by Adobe Edge

My current project involves using Adobe Edge Animate for the majority of my website, but I am looking to create a mobile version as well. In order to achieve this, I need to transition from onClick events to onTouch events. However, I am struggling to find ...

Employing jQuery to populate information into an input field, yet encountering an issue where the data is not being successfully transmitted to

On my page, there is a form with an input box <input name="bid_price" id="bid_price" class="form-control"> If I manually enter a value, it gets posted successfully But when I try to insert a value using jQuery, it doesn't get posted, even tho ...

The Material UI Elements are not displaying properly as expected. Instead of seeing the MUI elements, a tan box is appearing. What steps can I take to resolve this

I'm currently using Material UI to build a basic header, footer, and profile page. However, whenever I attempt to display any type of element (such as Menu, Appbar, Toolbar, IconButton, Tab, Tabs, etc.), the page only shows a tan box instead of the a ...

Change the hover effects on the desktop to be more mobile-friendly

In my desktop version, I have implemented some code that enables hovering over a button to display additional information or text. How can I modify this for mobile so that nothing happens when the button is tapped on? .credit:hover .credit-text, .credit- ...

An easy way to adjust the date format when linking a date in ng-model with md-datepicker

<md-input-container> <label>Scheduled Date</label> <md-datepicker ng-model="editVersionCtrl.selectedPlannedDate" ng-change="editVersionCtrl.checkPlannedDate()"> </md-datepicker> </md-input-container> ...

Erase every picture contained within the element

<div id="uniqueidhere"> <span> <span><img src="image1link"></img></span> <span><img src="image2link"></img></span> <span><img src="image3link"></img></span> <span>&l ...

Does jqgrid navgrid have an event called "on Refresh"?

Is there a way to trigger an event before the grid automatically refreshes? I am looking for something similar to "onSearch" but for the reset button. Below is the code snippet for the navgrid: $("#jqGrid").jqGrid('navGrid','#jqGridPag ...

Passing a component as a prop in Vue3 allows for more

I'm currently working on a tab component that requires passing a component as a prop. How can I properly display the component passed in the props? Here is my Tab.vue code: <template> <div class="bg-white flex items-center rounded-tr ...

Sequencing the loading of resources in AngularJS one after the other by utilizing promises

While working in a service, my goal is to load a resource using $http, store it in a variable, load a child resource and store that as well. I understand the concept of promises for this task, but I am struggling with how to properly implement it in my cod ...

Is there a way to programmatically emulate Firebug's inspect element feature using JavaScript?

Is there a straightforward method in JavaScript or any popular libraries like YUI or jQuery to allow users to interact with elements on a website, similar to the functionality of Firebug for developers? ...

Vue.js: Utilizing anonymous functions within props object

Looking to enhance the select2 example to make it more practical, I have added multiselect functionality and am now exploring custom select2 configuration options. Check out my progress on jsFiddle Encountering an issue where function properties of props ...

Challenges with Media Queries post Integration of MUI Library in React

I have been scratching my head for the last few hours. I needed to utilize a react library to design my dog app for a project. Opting for the MUI library, it has been effective for the grid layout. However, the challenge arises when attempting to implement ...

Steps to generate a script that tracks the number of visible divs and dynamically updates when a user applies a filter

I'm facing challenges in developing a script that can count and display the number of divs selected by the user compared to the total number of available divs. If you'd like to see the whole problem, check out this JSFiddle: http://jsfiddle.net/ ...

I'm having trouble understanding why my data does not appear even though there is space allocated for it automatically

I'm facing an issue with my code that is supposed to populate a table with dynamic content using react and ant.design. Everything seems to be working fine, but the actual content is not displaying - only the space for it is created. What's happe ...

Getting a Cookie in React from an Express JS API (MERN Stack)

My API in Express JS stores a token in a cookie on the client-side (React). The cookie is generated only when a user logs into the site. When testing the login API with Postman, the cookie is generated as expected: https://i.sstatic.net/rL6Aa.png However ...

Ways to verify if both the select and input fields are selected and populated with jQuery

In my html form, I have a select field and an input box with two classes being used: work_phone_class and work_phone_class_input. I am triggering ajax by clicking a save button with a specific class whenever the value in the select field is changed or any ...

React Star Rating Component: Issue with Image Display

To all who contributed their time and effort in responding to my previous question, I offer my sincerest apologies. Initially, I had assumed that assistance wouldn't be forthcoming, so I started working on the issue myself. As a result, I have made si ...

Is there a way to execute Javascript in Django without revealing static files?

When setting up payments on my Django site using Stripe, I realized that the .js file is visible under Sources in the developer tools when inspecting elements on any browser. This presents a potential security risk as anyone can access this file. How can ...