Passing only a single property through Vue store dispatch, rather than the entire

Is it possible to dispatch just a single property to the vuex store instead of the entire object? The vuex docs do not appear to provide any information on this. Currently, I am storing objects in the following manner:

store.dispatch('currentUser', {
  memberData: 'fooBar'
});

Sometimes, I only need to retrieve and push a single value from the database into the store. Is there a way to achieve this?

UPDATE

I believe my question may have been unclear.

In reality, what I need is the ability to access a nested child property within memberData during dispatch in order to modify just one element of memberData. In all honesty, the response value itself is irrelevant and could be 'foo' or anything else.

Answer №1

When exploring the documentation for dispatch, it becomes evident that the second argument, payload, can take on any form. It is not necessary for it to be an object-style dispatch nested within a property:

Dispatch:

store.dispatch('currentUser', response[0]);
  

Store:

state: {
    currentUser: undefined
  },
  mutations: {
    setCurrentUser(state, currentUser) {
      state.currentUser = currentUser;
    }
  },
  actions: {
    currentUser(context, payload) {
      context.commit('setCurrentUser', payload);
    }
  }
  

Mutation:

setCurrentUser(state, currentUser) {
    state.currentUser = currentUser;
  }
  

You can observe this concept in action through this example.

Update:

If your aim is to merge or update changes instead, you have the option of utilizing spread in object literals. This approach is also outlined in Vuex's documentation under Mutations Follow Vue's Reactivity Rules.

Dispatch:

store.dispatch('currentUser', { systemLanguage: response[0].systemLangId });
  

Store:

state: {
    memberData: { systemLanguage: 'foo' }
  },
  mutations: {
    updateCurrentUser(state, updates) {
      state.memberData = { ...state.memberData, ...updates };
    }
  },
  actions: {
    currentUser(context, payload) {
     context.commit('updateCurrentUser', payload);
    }
  }
  

An example showcasing this method is available for reference.

Hopefully, this information proves helpful!

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

I am leveraging AngularJS to display a modal window with Bootstrap and Spring servlet integration

In my project, I am utilizing AngularJS to display multiple pages. One of these pages contains a smart-table where I showcase the details of "users". When I wish to edit one of the users, I aim to display the edit page as a popup window. Below is an excer ...

What are the steps to incorporate metrics middleware for Socket IO notifications, specifically monitoring both emitted events and listener activity?

I am currently working on a project that involves tracking all socket.io notification transactions initiated by the server, resembling an API request/response counter to validate subscription validation. Our team is utilizing an express middleware to moni ...

Angular page not reflecting show/hide changes from checkbox

When the user clicks on the checkbox, I need to hide certain contents. Below is the code snippet: <input id="IsBlock" class="e-field e-input" type="checkbox" name="IsBlock" style="width: 100%" #check> To hide content based on the checkbo ...

What is the process to set a Woocommerce checkout field as optional when a checkbox is marked?

I need assistance with customizing the checkout page on Wordpress Woocommerce. Specifically, I want to make the field 'billing_name' not required if the checkbox 'buy_on_company' is checked. Currently, I have successfully hidden ' ...

The functionality of Bootstrap's JavaScript is dependent on the presence of jQuery - Reactjs framework

When attempting to combine bootstrap with reactjs, I encountered an issue with importing Bootstrap.js It seems that Bootstrap's JavaScript requires jQuery Below are some of the import statements I have tried: import $ from 'jquery'; imp ...

Including external JavaScript file

Is it expected to see "Test" in a message box following the code below? If not, what modifications should be done? <html> <base href="http://www.google.com"></base> <body> <script language="JavaScript" type="text/javascript" src ...

What sets apart express.Router() from using multiple express() objects?

When utilizing the latest express 4 router, it becomes possible to organize various route paths into separate files like the following example: // Inside cars.js const router = express.Router(); router.get('/brands', function(req, res) { ... } ...

The JQuery .on('Click') function is only functioning once instead of multiple times

Currently, I am in the process of creating a Like Button with a counter for my website. However, I am facing a challenge as I do not have a strong command over JavaScript and AJAX, which is necessary to correctly write the AJAX Request. The Ajax request f ...

Is it possible to make API calls without using the client side?

My website currently has the API Call implemented on the client-side JavaScript, which means that there is a risk of API Calls being used up by spam refreshes (or at least I assume so). As a newcomer, I am wondering if it's possible to make API calls ...

The 'exhaustive-deps' warning constantly insists on requiring the complete 'props' object instead of accepting individual 'props' methods as dependencies

This particular issue is regarding the eslint-plugin-react-hooks. While working in CodeSanbox with a React Sandbox, I noticed that I can use individual properties of the props object as dependencies for the useEffect hook: For instance, consider Example ...

Having trouble displaying a JSON response on an HTML page with AJAX

I've written an AJAX function that goes like this: function ajx(){ var ajaxRequest; // This variable makes Ajax possible! try{ // Works in Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // For Internet Exp ...

My content is being obstructed by a single-page navigation system

I attempted to create a simplified version of the issue I am facing. Basically, I am working on a header with navigation that stays at the top of the page while scrolling. The problem arises when clicking on a section in the navigation. The screen scrolls ...

How can one define a function type in typescript that includes varying or extra parameters?

// define callbacks const checkValue = (key: string, value: unknown) => { if (typeof value !== 'number' || Number.isNaN(value)) throw new Error('error ' + key) return value } const checkRange = (key: string, value: unknown, ...

Using ng-fullcalendar in Angular, it is necessary to display events that have specific identifiers

I'm working on implementing a scheduler in my Angular application using ng-fullcalendar. Installation was smooth, but now I want to render only specific IDs from my event.service. Here's what my ngOnInit function looks like: ngOnInit() { t ...

Is it possible to maintain inline style modifications when the dispatch event is triggered?

I am encountering an issue with inline style changes that are reset once my dispatch function is completed. Even though the rest of my component's functionality is working fine (the counter continues to run), the state is being re-rendered. Here is a ...

An illustration of the fundamental concepts of require.js

main.md <markdown> <head> <script data-main="script" src="http://requirejs.org/docs/release/2.1.8/minified/require.js"></script> </head> <body></body> </markdown> script.css defi ...

Editing JSON files - Substitute the $scope object with a new value

I am facing an issue with extracting data from an external JSON file for my application. Although most of the data loads into a DataTables table successfully, I am encountering problems with the $scope variable since it is referencing external data. Specif ...

Error message encountered in React Material-UI while attempting to run the basic Hello World test - Invalid hook call

My plan is to kick off a project using React and Material-UI. I decided to start with the simplest Hello World example based on the official documentation, in a brand new Ubuntu OS. However, when I tried running the code, an error popped up! Here's wh ...

CSRF validation did not pass. The request has been cancelled. The failure occurred due to either a missing or incorrect CSRF token

Upon hitting the submit button in the login form, I encountered the following error message: Forbidden (403) CSRF verification failed. Request aborted. CSRF token missing or incorrect. settings.py MIDDLEWARE = [ 'django.middleware.security.Secur ...

Verify that the API call query parameters do not contain any null values

I need to figure out how to submit only the queryParams parameters with values that are not null. Here's the current code for the submit function: onSubmit() { const queryParams = '&name=' + this.name + ...