Vuex: Clearing all data from the store and setting it back to an empty array

My Vuex store has a state variable called scannedList, which is initially set to an empty array []:

state: {
  scannedList: []
}

I have a mutation that adds ids to this array. I attempted to clear the entire state by using:

store.commit('addToScannedList', [])

However, the ids are still present in the array. What is the correct way to achieve this?

Answer №1

To clear an array by pushing an empty array onto it is not effective. Instead, you can create a new mutation like this:

clearScannedItems(state) {
  state.scannedList = [];
}

Alternatively, you can modify your existing mutation to handle clearing the array when no payload is passed:

addScannedItem(state, item) {
  if(item !== undefined) {
    state.scannedList.push(item);
  } else {
    state.scannedList = [];
  } 
}

You can trigger this mutation using store.commit('addScannedItem') without passing any payload.

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

tips for dynamically changing the active class

The issue lies in the code at ul-> li -> a ng-click. The "active" class is not changing when clicked. This is the content of my index.html file <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <head lang="en"> <me ...

Why is my event.target.value not updating correctly in React useState?

My problem is that when I use useState, I am receiving incorrect numbers For example, if I print e.target.value it might display 1, but my selectedIndex shows 2. Similarly, when I have a selectedIndex of 0, it retrieves something different like 1. Any tho ...

Minimizing switch-case statement into inactive input field (AngularJS)

While the code statement functions as intended, it may not be considered best practice due to the repetition of variables in each case of the switch statement. I am unsure of how to streamline the code or if there is an alternative to using a switch statem ...

Extracting the content within HTML tags using regular expressions

There is a specific string structure that needs to be processed: <div class="myClass"> Some Text. </div> <div class="otherClass"> Some Text. </div> The task at hand involves parsing the div with myClass and replacing certa ...

difficulty using workbox to cache content in vue-cli

In my development setup with vue-cli v3.0.0.beta10 and the default integrated workbox, I included this configuration in my vue.config.js file (located in the root folder): pwa: { //pwa configs... workboxOptions: { // ...other Wor ...

What steps can be taken to ensure that the JavaScript fetch() function is consistently accessing and updating the new data that is being added to the

I'm facing an issue with my JSON data file that I want to display on the screen. I have a function with a fetch() request that executes every time I click a button. Initially, it works perfectly by fetching the data and displaying it correctly. Howeve ...

The notify.js fails to display notifications in the event of an error

Why am I not receiving any error notifications even when there is an error message in the response object? $.ajax(settings).done(function (response) { if ( "error_message" in response ) { console.log(response); $.notify(" ...

What is the method for obtaining the ID of a dynamically generated DropDownList and verifying if its value has been altered?

I successfully created a view with the help of Steven Sanderson's blog where a dynamic number of textboxes and DropDownList are generated. Everything is functioning properly. However, I would like to ensure that the form cannot be submitted until each ...

Functionality of jQuery Mobile Page

$(document).on("pageshow", "#mappage", function (event) { $("#doldur").append("<li> <label onclick='getBina(" + featuressokak[f].attributes.SOKAKID + ");'>" ...

What is the best way to apply a hover effect to a specific element?

Within my CSS stylesheet, I've defined the following: li.sort:hover {color: #F00;} All of my list items with the 'sort' class work as intended when the Document Object Model (DOM) is rendered. However, if I dynamically create a brand new ...

Dimensions of Bootstrap carousel

I am attempting to create a Bootstrap carousel with full-width images (width: 100%) and a fixed height. However, when I set the width to 100%, the height automatically takes on the same value. I am unsure if the issue lies within my files. <div id="m ...

Extracting data from website's table using JavaScript and opening the link in href

My goal is to extract the details page for each link found on this particular page. The link provides access to all the information required: PAGE However, I'm interested in extracting details from pages that have links like this: href="javascr ...

When attempting to bind various data to a single div using knockout js, the issue of duplicate records appearing arises

I am encountering an issue with a div that is set up to display 10 records at a time. When the user clicks on the next link, the next set of 10 records should be loaded from the server. However, after binding the newly added records, they are being shown m ...

Is it possible to pass hooks as a property value from a useEffect in React.js?

As a newcomer to React, I am struggling to grasp the concept of how to use useEffect effectively. In one of my components, I have an array of objects stored in a file named cardData.js. These objects are used to automatically create cards, each containing ...

Is it possible for an object hidden in the DOM using jQuery to magically reappear when you click the back button or use the bfc

Is there a way to prevent a box from reappearing when using the back button on a webpage? On my website, there is a box that shows up on the first visit. However, when navigating using the back buttons on the site or the browser back button, the box appea ...

What is the solution to fixing the error message "SyntaxError: missing ) after argument list" in JavaScript?

I wrote some code in HTML/JavaScript/PHP, and in the 3rd echo statement, I added a button that calls the deleteAttribute() function when clicked. However, I encountered an error at the 3rd echo statement with (type = "button"): "Uncaug ...

Retrieve the title of the page that triggers the loading of another page through JQuery

My ASP.NET/C# page utilizes jQuery to load a Navigation.aspx page which includes my header and navigation buttons: <script> $(function(){ $("#nav-placeholder").load("Navigation.aspx"); }); </script> Within t ...

What is the best way to send an array of numbers by utilizing e.target.value and React useState Hooks?

Trying to update the numbers array by passing it into submitNumbers() and using an onChange event. Issue arises when trying to use useState() to handle the array. When attempting to log these constants while the code is running, they are returning as not ...

Is it possible for an Ajax request to finish before the DOM finishes loading?

Currently, I am fetching data via a jQuery Ajax request and presenting it on the page. In order to properly display the information, I have to make sure that both the DOM is fully loaded and the Ajax call has been completed. Is there a scenario where an ...

Obtaining the id in JavaScript from PHP to display information

This is the code I've written: <textarea id="summernote<?php echo $u->id ?>" name="isi" placeholder="Write here" style="width: 100%; height: 100px !important; font-size: 14px; line-height: 18px; border: ...