Following the execution of the commit, the variable remains in a state of

I'm currently in the process of creating a new task.

I was expecting the result to be 'created task with title', but the title is empty because 'formData' is still reactive.

Below is an example of the code:

// index.vue
<form class="form" @submit.prevent="createTask()">
  <label class="form__label">
    Title:
    <input
      type="text"
      class="form__input" 
      v-model="formData.title"
      placeholder="Title"
      required
    />
    </label>
  <button type="submit" class="form__button">Create</button>
</form>

...

export default class App extends Vue {
  formData = {
    title: "",
    completed: false,
  };


  createTask(): void {
    store.commit("task/SET_TASK", this.formData);
    this.formData.title = "";
  }
}

// Task.ts (vuex)
export default class Task extends VuexModule {
  tasks = [
    {
      title: "Example task",
      completed: false,
    },
  ];

  @Mutation
  SET_TASK(payload: TaskInterface) {
    this.tasks.push(payload);
  }
}

Can you help me identify what I might be doing wrong?

Answer №1

To avoid this issue, you can work around it by utilizing the spread syntax to generate a new object.

addNewTask(): void {
    store.commit("task/SET_TASK", { ...this.formData });
    this.formData.title = "";
  }

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

Using the Google Identity Services JavaScript SDK in conjunction with Vue and TypeScript: A comprehensive guide

I am currently working on authorizing Google APIs using the new Google Identity Services JavaScript SDK in my Vue / Quasar / TypeScript application. Following the guidelines provided here, I have included the Google 3P Authorization JavaScript Library in ...

Tips for updating state in React TypeScript 2.0?

Working with a small component built using React and TypeScript has presented a unique challenge. interface Props { } interface State { isOpen: boolean; } class App extends React.Component<Props, State> { constructor(props: Props) { super ...

Navigating through a React application with several workspaces - the ultimate guide

Currently, I am working on implementing a monorepo setup inspired by this reference: https://github.com/GeekyAnts/nativebase-templates/tree/master/solito-universal-app-template-nativebase-typescript In this repository, there are 4 distinct locations wher ...

"I'm looking for a way to store and fetch TypeScript objects with PouchDB. Any suggestions on

As someone who is new to typescript and web development, I am eager to incorporate PouchDB into my typescript project to store my objects. Despite the lack of documentation, I am struggling to find the correct approach. I have created typescript objects t ...

using mapState to set state

My goal is to streamline the process of assigning setter methods using mapState. Currently, I am employing a workaround where I temporarily name the variable of interest (todo) as storetodo and then reference it in another computed variable called todo. m ...

When using firebase serve, typescript code is not being compiled prior to initiating the server

My typescript firebase function project is simple yet the code works fine. However, there seems to be an issue in the project configuration that causes firebase serve NOT to recompile the code before starting the server. On the contrary, firebase deploy wo ...

Create a Vue component that utilizes the v-for directive to iterate through a list of items, passing data from a

Imagine having two arrays with a similar structure like this: const individuals = [{name: "Jane Doe", id: "0001"},{name: "John Doe", id:"0002"}, {name: "Sam Smith", id: "0003"}, {name: "Joe ...

Encountering an error with the Angular 2 SimpleChanges Object during the initial npm start process

Within my Angular 2 application, there exists a component that holds an array of objects and passes the selected object to its immediate child component for displaying more detailed data. Utilizing the "SimpleChanges" functionality in the child component a ...

Ways to implement a filter pipe on a property within an array of objects with an unspecified value

Currently, I'm tackling a project in Angular 8 and my data consists of an array of objects with various values: let studentArray = [ { Name: 'Anu', Mark: 50, IsPassed: true }, { Name: 'Raj', Mark: 20, IsPassed: false }, { Na ...

Tips on rotating a material-ui icon

Having trouble rotating a material-ui icon using the CSS animation property. Can anyone assist in identifying what may be causing the issue? Link to example code sandbox I'm looking for a continuously rotating icon. ...

Retrieving information from the database and mapping it to an array

Currently in my Angular application, I have hardcoded values in an array and bound them to a dropdown list using the in-built control, which is functioning correctly. The current setup looks like this: export class UserDetailsComponent implements OnInit { ...

Preserve the checkbox state upon refreshing the page

I am facing an issue with keeping the checkbox state saved after a page reload. Currently, I have stored my unchecked checkboxes in localStorage, but I am unsure about what steps to take next. In simple terms, I want the checkbox to remain unchecked when I ...

Is Vue instance created if element is present?

I am currently developing an application where some pages contain Vue instances, while others do not. My issue arises when switching from one page to another, and the following warning message appears: [Vue warn]: Cannot find element: #element-id How can ...

Is it possible to enable password authentication on Firebase even if the user is currently using passwordless sign-on?

In my frontend JS project, I have integrated Firebase for web and am utilizing the passwordless (email link) authentication method for users. I am now interested in implementing password sign-on for an existing user who is currently using passwordless si ...

When the *ngFor directive disrupts the CSS Grid Layout, resulting in all items being displayed in a single column

I am a beginner in the world of programming and web development. Currently, I am working on building my own personal website. My goal is to arrange boxes in a grid with 4 columns, similar to the layout you can find at this link: Each box represents an ob ...

Using the useRef hook in a TypeScript project to retrieve a boolean value

As I work on developing an application using Nextjs, I have encountered an issue while using react useRef with typescript. The problem arises when I use useRef without typescript, everything works smoothly. However, the moment I include HTMLDivEleement as ...

Hiding the line connector between data points in ChartJs

I recently took over a project that includes a line chart created using Chart.js by the previous developer. My client has requested that I do not display a line between the last two data points. Is this possible with Chart.js? I have looked through the doc ...

Creating a new TypeScript file via the command line: A step-by-step guide!

When I want to create a new file named main.ts, I try to write the command but it keeps showing an error. "bash: code: command not found" https://i.stack.imgur.com/cpDy3.png ...

converting objects into an array

I am currently working on a project using JavaScript/Vue where I am displaying ingredients by mapping each item and joining them with ", ". However, I now want to display each item as a list but I am unsure of how to achieve this. The current code I have i ...

What is the best way to implement a delay in axios requests within a loop array?

I am currently working on a project in Vue where I need to add a delay to axios requests within a loop involving an array. let promises = []; for (const item of this.itemsWithIndex) { const cmd = "od_kioskPaperUpdate"; ...