Issue with Nuxt JS Vuex data not updating in real-time upon page load

Within my Nuxt JS 2.9 application, I have implemented Vuex to store and retrieve data in my layout. The issue I am facing is that when I set data into the Vuex store on a page-specific basis, it requires me to refresh the page in order to see the updated data, otherwise it displays outdated information.

For instance, on the Homepage, I use the following code:

mounted () {
  this.$store.commit('toolbar/setToolbar', {
    name: 'Homepage'
  })
}

And on the Settings page:

mounted () {
  this.$store.commit('toolbar/setToolbar', {
    name: 'Settings'
  })
}

This data gets stored in Vuex like this:

export const state = () => ({
  toolbarData: [{
    name: 'Homepage'
  }]
})

export const mutations = {

  /*
   * Toolbar data
   */
  setToolbar (state, data) {
    const settings = {
      name: data.name != null ? data.name : ''
    }

    state.toolbarData[0] = settings
  }

}

When transitioning between the Homepage and Settings pages, the state updates accordingly but does not reflect in real-time on the page itself.

I have attempted switching the lifecycle hook from mounted to created and beforeCreate hoping that it would commit to the store before the page finishes loading, but the issue persists.

Answer №1

Understanding reactivity in Vue comes with its own set of challenges. To dive deeper into this topic, check out: https://v2.vuejs.org/v2/guide/reactivity.html

If you're facing an issue, one way to tackle it is by replacing the entire array:

setToolbar (state, data) {
    const settings = {
      name: data.name != null ? data.name : ''
    }

    state.toolbarData = [settings]
  }

Another option is to selectively update specific elements:

import Vue from 'vue';

etToolbar (state, data) {
    const settings = {
      name: data.name != null ? data.name : ''
    }

    Vue.set(state.toolbarData, 1, settings);
  }

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

Express server unable to process Fetch POST request body

I'm currently developing a React app and I've configured a basic Express API to store user details in the database app.post("/register", jsonParser, (req, res) => { console.log("body is ", req.body); let { usern ...

What is the best way to obtain the id of an HTML element that is generated within jQuery code?

Typically, data is retrieved in HTML by storing the html in the html file. In my case, however, I create the html element inside jQuery. So, how do I call out the div id? How can I replace document.getElementByID ("edit").innerHTML=.... when the element i ...

Utilizing AXIOS in React functional components for REST API integration

When trying to parse Rest API responses with JSON using AXIOS in a functional component, the issue arises where it initially returns an empty array before displaying the exact API response data after rendering. This can be confusing as the return function ...

Can we rely on the render method to display the updated state immediately after invoking setState within

Is it guaranteed that the state will exist in the render method if I call setState within componentWillMount without using a callback? According to Facebook, "componentWillMount is called before render(), therefore calling setState() synchronously in this ...

Is it possible to add data in MongoDB without specifying a field name?

I have a couple of queries that revolve around the same concept: If I want to insert a new 'row' in MongoDB, can I do so by specifying the order of the fields? For instance, if my collection looks like items = { { name: "John", age: "28" ...

Having difficulty retrieving the value of a form using javascript

I've been struggling to retrieve the value of a form using JavaScript, but for some reason it keeps coming up as undefined, almost as if the input doesn't exist. Here's the HTML snippet: %@ Language="VBScript" CodePage=65001 %> ... Th ...

"Efficiently setting up individual select functions for each option in a UI select menu

I've integrated UI Selectmenu into my current project UI selectmenu includes a select option that allows for setting select behavior across all selectmenu options, as shown in the code snippet below: $('.anything'). selectmenu({ ...

Inconsistent CSS3 transitions behavior in Firefox 4

I have been playing around with some css3 transitions and created a slider test. It works perfectly in webkit browsers, but I have noticed an issue in Firefox 4. When clicking on the left link for the first time, the slider is supposed to slide to the left ...

Guide on replacing placeholders in a text with input fields and connecting them with v-model in Vue.js

I am currently working on creating an exam page that features fill in the gap type questions. These questions have placeholders where students need to insert the correct words or phrases. The questions are fetched via an ajax request and consist of simple ...

The process of uploading images with VueJS and Laravel via API is experiencing difficulties

I am currently facing an issue with uploading images using Vue.js and Laravel via API. When I send the image information from Vue.js to Laravel through the API, it seems that Laravel is not receiving the data in the correct format, resulting in an error be ...

Leveraging nuxt-link functionality within an external component library

In our efforts to develop an NPM package containing vue-based components for various Nuxt applications, there is a particular challenge we are facing with the "nuxt-link" component integration. Consider a scenario where we have a component such as a produ ...

Form Automatically Submits Data Upon Loading of Page

I am currently facing an issue with my hidden div that is being loaded when I click the submit button. The form is sending results upon page load, and even though I have included the validateForm() function and called it at the end of the submit function ...

What is the best way to use React to display all user post images on a webpage in real

I'm currently working on a React web application where I want to display images in all user posts on my Discover component. The JSON objects look like this: https://i.stack.imgur.com/ZM6Lu.png This is the current state of my discover component. Dis ...

An unexpected identifier error occurred following an Ajax request

Below is the HTML code that I am working with: <div id="texttheory" class="centertext">'. $short .' </div>'; <button id="thbutton" class="theory_button" onclick="javascript:changetheory('.$long.')"> <im ...

The challenge of maintaining scope in AngularJS Bootstrap Modals

In my application, I am using ngRoute to load a template that includes a bootstrap modal dialog. The purpose of this modal is to prompt the user if they are sure about losing their changes when trying to leave the row they are currently editing in a table. ...

Is there an easy way to determine if an element inside a Vue component is overflowing?

I created a unique component called ResultPill which includes a tooltip feature (implemented using vuikit). The text displayed in the tooltip is determined by a getter function called tooltip, utilizing vue-property-decorator. Here are the relevant parts o ...

Exploring the implementation of async.each with request in the Node.js Express framework

I'm currently working on a node.js project where I need to send multiple API requests in an iterative manner, so I decided to use the async.each method for this purpose. However, I encountered an issue where the console reported that the 'url&ap ...

Automatically injecting dependencies in Aurelia using Typescript

Recently, I started working with Typescript and Aurelia framework. Currently, I am facing an issue while trying to implement the @autoinject decorator in a VS2015 ASP.NET MVC 6 project. Below is the code snippet I am using: import {autoinject} from "aure ...

What is the time stamp format of 1651928421543667000?

Recently, I have encountered an issue with an API returning a timestamp as 1651928421543667000. Despite trying various PHP functions like strtotime(), datetime(), and strftime(), I am unable to find the correct format for it. Can anyone provide some guid ...

Issue with create-react-app and express server not displaying correctly in Internet Explorer

My application functions perfectly with Chrome and Safari. It utilizes React for the front-end and Express for the back-end. However, when I try to open it in Internet Explorer, all I see is a blank white page. Additionally, I encounter this error message: ...