Action type is not recognized: modification

After browsing through multiple forums, I haven't found a solution that works for my particular issue with my VueJS app. The problem arises when I try to input something in my first component. Below is the code snippet:

main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

store.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

export const store = new Vuex.Store({
    state: {
        item: ''
    },
    mutations: {
        change(state, item) {
            state.item = item
        }
    },
    actions: {
        change({ commit }, newValue) {
            commit('change', newValue);
        }
    },
    getters: {
        item: item => state.item
    }
})

App.vue

<template>
  <div id="app">
    <AddItem/>
    <DisplayItem/>
  </div>
</template>

<script>
import AddItem from './components/AddItemComponent'
import DisplayItem from './components/DisplayItemComponent'

export default {
  name: 'App',
  components: {
    AddItem,
    DisplayItem
  }
}
</script>

AddItemComponent.vue

<template>
    <div>
        <label for="item">Item Name</label><br/>
        <input type="text" name="item" @input="changed">
    </div>
</template>

<script>
export default {
    methods: {
        changed: function (event) {
            this.$store.dispatch('change', event.target.value)
        }
    }
}
</script>

DisplayItemComponent.vue

<template>
    <div>
        <p>Item Name : {{ $store.getters.item }}</p>
    </div>
</template>

...and it's accompanied by an error

https://i.stack.imgur.com/Dm9Mi.png

Despite trying numerous approaches, none of them seem to resolve the issue. Could it be that I missed something?

Thank you for your assistance in advance.

Answer №1

To accomplish your task, you must first dispatch to an action, and within that action, commit the value which will trigger your mutation. It is crucial that your state is represented as an object rather than a function.

Below is a simple example for your reference:

export const store = new Vuex.Store({
    state: {
        item: ''
    },
    mutations: {
        change(state, item) {
            state.item = item
        }
    },
    actions: {
        change({ commit }, modifiedValue) {
          // implement necessary logic here
          commit('change', modifiedValue)
        }
    },
    getters: {
        item: item => state.item
    }
})

Following this setup, you should be able to execute your dispatch like so:

this.$store.dispatch('change', event.target.value)

Answer №2

At last, I have pinpointed the issue... Turns out I was importing the incorrect store. Here is the corrected code after some troubleshooting!

main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/store'

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

A big thank you for all the assistance provided!

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

Transfer information from the client to the server using AJAX and PHP by

When attempting to post a JavaScript variable called posY to a PHP file, an error occurred with the message: Notice: Undefined index: data in C:\xampp\htdocs\Heads_in_the_clouds\submitposY.php The posY variable is defined in the JavaSc ...

Achieving data concurrency in VueJS: Best practices and implementation techniques

Currently in the process of creating a form for candidates to submit their recruitment information, with basic details being stored in the candidate information table and their CV file saved within Strapi CMS's archive. However, I am encountering an i ...

The text box's word limiter is malfunctioning when writing code on a child page of the master

Encountering an unexpected problem that I never thought would happen. I wrote a word limiter code for a text box on a single page and it was working perfectly fine. The word limiter worked and the remaining words were displayed by a label named "remaining6 ...

Why is Jasmine throwing an error when I try to use getElementsByTagName(...)?

HTML: <ul id="listONE"> <li class="{{isSel}}" ng-repeat="person in people" ng-click="selPersonToChange(this)">{{person.name +" - "+ person.city}}</li> </ul> A snippet from my script.js using AngularJS (1.3.1): mymod.control ...

Tips on choosing one specific JSON object from a group and exploring its structure in Python

Looking for guidance to access a specific javascript element named SOURCE.pdp.propertyJSON and extract its attributes in a Pythonic way from a webpage filled with different script elements. Browse through the HTML source code below to get an idea and then ...

Different ways of manipulating images with JavaScript

I attempted to tackle the issue independently, but I'm stumped. Is there a way to adjust the width of an image in JS when the screen dimensions are changed? ...

Shrink video size directly in the browser using JavaScript and HTML5 before storing or sharing it

Is there an optimal method for resizing and potentially trimming a video using Javascript, HTML5 or Webassembly in modern browsers before saving it to the Filesystem? What is the most effective way to edit a video within the browser? Here are some differe ...

Steps for creating a new tab and refreshing the address bar with a different URL without having to reload the current page

I'm trying to create a functionality on my page where clicking a button will open a new tab with a modified URL, all without reloading the current page. Below is the code that I'm using when the button is clicked: function changeUrl(){ var pri ...

Error on Network: 400 BAD REQUEST in Ionic framework

I recently implemented push notifications successfully, but I am facing a network error with a 400 bad request when trying to access a specific API endpoint. The error message states: "NetworkError: 400 BAD REQUEST - https://apps.ionic.io/api/v1/app/77c3 ...

Deactivate certain days in Material UI calendar component within a React application

Currently, my DatePicker component in React js is utilizing material-ui v0.20.0. <Field name='appointmentDate' label="Select Date" component={this.renderDatePicker} /> renderDatePicker = ({ input, label, meta: { touched, error ...

Incorporating the non-typescript npm package "pondjs" into Meteor applications using typescript files

Implementing the Pondjs library into my project seemed straightforward at first: meteor npm install --save pondjs However, I'm encountering difficulties when trying to integrate it with my Typescript files. The documentation suggests: In order ...

Using track by in ng-repeat function triggers an endless $digest-loop issue

It appears that I am still struggling to grasp the mechanism behind ng-repeat, $$hashKeys, and track by. Currently, in my project, I am working with AngularJS 1.6. The Issue: I have an array of complex objects that I want to display as a list in my view ...

Encountering challenges with integrating GraphQL and the Magic API. Seeking guidance on implementing mutations and queries on a server to establish a schema

In my project, I have organized my GraphQL operations into two separate files: mutations.js and queries.js, which are stored in a directory named gql... The gql folder serves as a repository for GraphQL mutations and queries that are utilized throughout t ...

Generate selectable options dynamically in real-time

In my code, I am working with two select elements - one filled with data and the other one empty. The idea is that when a selection is made in the first select element, I use a switch statement to capture that event. Based on the value selected, I then aim ...

Creating a way for a Node and React application to share classes

I am in the process of developing a Node and React application, both implemented using TypeScript. The directory structure of my project is illustrated below: https://i.sstatic.net/914A1.png My query: Given that I am utilizing the same programming langu ...

In the world of Node.js and Java, the concepts of "if"

Here is a code snippet that I am working with: var randFriend = friendList[Math.floor(Math.random() * friendList.length)]; if (randFriend == admin) { //Do something here } else if (randFriend != admin) { client.removeFriend(randFriend); } I am tr ...

Executing multiple nested $http calls in Angular can significantly slow down the process

Within my angular application, I am currently dealing with 6 to 7 http chaining requests that are taking a significant amount of time to execute. What are some strategies for optimizing this process? empSvc.getallEmp().then(function (data) { if (dat ...

The video auto plays in a pop up window, but if the pop up doesn't appear, I can still hear the audio

I recently added a pop-up to my website and attempted to include autoplay functionality. However, I encountered an issue where the sound would still play even if the pop-up did not appear upon refreshing the page. How can I prevent the autoplay when the po ...

Incorporating the Material UI App Bar alongside React Router for an enhanced user

For my web development course project, I am venturing into the world of JavaScript and React to create a website. Utilizing Material UI's appbar for my header design, I have successfully incorporated react router to enable navigation on my site. Howev ...

JSDOM failing to retrieve complete list of elements from webpage

I'm currently struggling with a simple webscraper using JSDOM. Here's the code snippet I've been working on: const axios = require("axios"); const jsdom = require("jsdom"); const { JSDOM } = jsdom; let v = "15" ...