Displaying items from an array is made easy thanks to the combination of mapGetters, VUEJS, vuex, and vuetify

My website has two main routes: one for the home page and another for a configuration panel.

In the home page, there is a container displaying information such as date, time, current temperature, etc. Below that, there is another container where users can input daily information. Users can input text and set a timeout value (in seconds) for how long the message should be displayed. Using Vuex, I set the input values to states - one array for strings (text input) and another array for integers (timeout value). For example, ['hello', 'how are you'] and [12, 14].

My issue is that I want to display messages one at a time based on their respective timeout values. Each message should disappear when its time is up and the next message should appear in sequence.

Here is the code snippet:

<template>
<body>
  <div class="container">
    <table>
      <thead>
        <tr>
          <th scope="col" class="colc">Date</th>
          <th scope="col header" class="colc">Time</th>
          <th scope="col header" class="colc">CELSIUS</th>
          <th scope="col header" class="colc">WINDSPEED</th>
        </tr>
      </thead>
      <tbody>
        <tr class="priority-200">
          <td id="writeDay" class="default">{{ createdDate }}</td>
          <td id="hour" class="default">{{ createdHours }}</td>
          <td id="degree" class="default"></td>
          <td id="speed" class="default"></td>
        </tr>
      </tbody>
    </table>
    <div class="container2" v-show="elementVisible">
      <h1 v-for="m of message" :key="m" class="info">
        <span>{{ m }}</span>
      </h1>
    </div>
  </div>
</body>
</template>

<script>
import moment from "moment";
import { mapGetters } from "vuex";

export default {
  name: "Home",
  data() {
    return {
      elementVisible: true
    };
  },
  computed: {
    ...mapGetters(["message", "sec"]),

    ...mapGetters({
      message: "message",
      sec: "sec"
    }),
    createdDate() {
      return moment().format("DD-MM-YYYY ");
    },
    createdHours() {
      return moment().format("HH:mm ");
    }
  },
  mounted() {
    this.$store.dispatch("SET_MESSAGE");
    this.$store.dispatch("SET_SEC");

    setTimeout(() => (this.elementVisible = false), this.sec);
  }
};
</script>

Currently, all messages are displayed simultaneously and disappear immediately. I want to display only one message at a time according to its corresponding timeout value until it expires, then move on to the next message sequentially.

StoreJS content:

const state = {
  message: [],
  sec: +[],
};

const getters = {
  message: (state) => {
    return state.message;
  },

  sec: (state) => {
    return state.sec;
  },
};

const actions = {
  setMessage: ({ commit, state }, inputs) => {
    commit(
      'SET_MESSAGE',
      inputs.map((input) => input.message)
    );

    return state.message;
  },

  setSec: ({ commit, state }, inputs) => {
    commit('SET_TIMEOUT', inputs.map((x) => x.sec).map(Number));
    return state.sec;
  },
};

const mutations = {
  SET_MESSAGE: (state, newValue) => {
    state.message = newValue;
  },

  SET_TIMEOUT: (state, newSecVal) => {
    state.sec = newSecVal;
  },
};

export default {
  state,
  getters,
  actions,
  mutations,
};

Answer №1

The main issue lies in the key

  <h1 v-for="m of message" :key="m" class="info">          ----> at present, all values are displaying simultaneously and disappearing immediately after because it is not recognizing the sec

Each message must have a unique key that is a string, but it seems you are passing an object instead. If your messages have an id, you can utilize it like this

  <h1 v-for="m of message" :key="m.id" class="info"gt;  

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'm getting a JS error saying that the variable "var" is not defined. Does anyone know how I can

Here is the code I am using to dynamically create a sitemap.xml file when accessing /sitemap.xml database = firebase.database(); var ref = database.ref('urls'); ref.on('value', gotData, errData); function errData(err){ ...

Is there a way to allow for clicking on a row to redirect to a new tab and display the data of the selected row?

Currently, I am working with mui-datatable and trying to figure out how to enable the user to be directed to another page simply by clicking on a row. Additionally, I need the data of that specific row to be passed along to the new page as well. The error ...

Retrieve the next 14 days starting from the current date by utilizing Moment.js

Looking to display the next 14 days in a React Native app starting from today's date. For example, if today is Friday the 26th, the list of days should be: 26 - today 27 - Saturday 28 - Sunday 01 - Monday 02 - Tuesday ............ 12 - Friday Is ther ...

Sending an image file using AJAX and jQuery

I am currently using Mustache JS to generate a template called 'addUser1' for display purposes. However, when I execute this code, only the image location is being sent to the server, not the actual image itself. What could be causing this issue? ...

Ways to enhance the type definitions for a built-in HTML element in Vue.js?

Imagine having an element that wraps around an input and inherits all of its properties, along with some extras. In React, you would define this as: interface ExtendedInputProps extends React.ComponentPropsWithoutRef<'input'> { some: T ...

Tips for passing parameters to an ajax request in Django URL?

In my current setup, I'm passing a URL to the ajax like this: $('.togglebtn').on("click",function(){ console.log("Hello") $.ajax({ type: 'GET', url: "http://loc ...

Why is it that using "return false" does not stop the page from reloading when clicked?

I've attempted using this code, but it doesn't seem to prevent the page from reloading when the onclick function is triggered. What am I missing here? Could this be a problem related to browser compatibility? function track_specs() { $('b ...

The file 'index.js' does not have a default export

Stepping into the world of MERN development, I embarked on a journey to construct a learning management system based on an outdated tutorial. To my dismay, I encountered the following error: > ..\server\middlewares\index.js:1 > import ...

When using Vuejs2, the list of autosize textareas connected to an expanding array of objects does not properly update their values

After binding https://github.com/jackmoore/autosize to textareas within a v-for loop, I've noticed an unexpected data persistence issue while expanding the array linked to that list: While inputs on the left side move downward as intended, new textar ...

Trouble with Bootstrap 5 Dropdown Functionality

Currently, I am using Bootstrap 5 in conjunction with Django to create a website and I'm encountering difficulties with getting a dropdown feature to work properly. Despite copying the code directly from w3schools, it fails to function when I load the ...

Node.JS 3DES encryption encountering an issue with IV length validation

I'm new to working with Node.js and I've encountered an issue with the encryption object: var des3_key = new Buffer("redacted", "base64"); // obtained from another source var des3_iv = new Buffer("alsoredacted", "base64"); // obtained from anoth ...

Angular: Preserve the URL even when encountering a 404 page

Creating a custom 404 page in Angular 4 is something I have recently done, and I am looking for a way to preserve the incorrect URL that was input by the user. To see an example of this behavior, you can visit sites like GitHub. They show a 404 page when a ...

Firebase allows for the updating of an object within a nested array

Within Firestore, I have a Document that includes a property named "items" which is of type array. This array consists of ShoppingItem objects with the specified structure: export class ShoppingItem { id?: string; name: string; checked = false; } To ...

Effortlessly altering values within a dynamic key in Firebase's real-time database

I attempted to redefine the value as "pretend" using the code snippet below, but unfortunately it did not succeed. dataBase.ref().orderByChild('en_word').equalTo('pretend').set({ en_word: 'Pretend' }) ...

Issues with Jquery Checkboxes Functionality

Hi everyone, yesterday I had a question and since then I have made quite a few changes to my code. Right now, I am attempting to make some JavaScript work when a specific checkbox is checked. However, nothing is happening when I check the checkbox. Can any ...

Transmit information to PHP script using JavaScript

I am facing an issue with sending form data to another PHP file. While the process is working, the problem lies in the fact that once the data is submitted, the page does not redirect to the specific page as intended. It seems like it doesn't work th ...

How can I duplicate or extract all the formatting applied to a specific text selection in Ckeditor?

I am currently utilizing CKEditor version 3.6 within my Asp.net MVC 3 Application. One of my tasks involves creating a Paint format option similar to Google Docs. I am looking to integrate this feature into CKEditor. Is there a way in CKEditor to transfe ...

Passing specific props to child components based on their type in a React application using TypeScript

Sorry if this question has already been addressed somewhere else, but I couldn't seem to find a solution. I'm looking for a way to pass props conditionally to children components based on their type (i.e. component type). For example, consider ...

What is the proper way to safely close the pg-promise connection in a node.js environment once all jest tests are completed?

I am facing a challenge in closing a PG-Promise database connection after running a function test in Jest. The database connection is initialized in one central location (db.js) and required in multiple places. In this scenario, it is being used by seed.j ...

Logging in with oidc-client and IdentityServer4 on separate domains

I am currently working on a VueJs application hosted on localhost which utilizes the oidc-client.js library for logging in to an IdentityServer4 server located in a production environment on another domain. Upon successful login, I am redirected back to t ...