Vue Pinia ensures that reactive state is only updated once, preventing unnecessary updates

In my Vue application, the structure is as follows: App.vue -GroupWrapper --GroupListing -PeopleWrapper --PeopleListing -ConversationWrapper

Within my user store that utilizes Pinia, I primarily call user.findChats() in the App.vue component.

Code snippet from App.vue:

import { useChatStore } from './stores/chat'
import { useUserStore } from './stores/user'

const chat = useChatStore()
const user = useUserStore()

chat.findChats();

This function finds all chats and categorizes them into either "dm" or "gm" type, storing them in respective arrays.

Snippet from chat.js:

import { defineStore } from 'pinia'
import { db } from '../firebase'
import { collection, query, where, getDocs } from 'firebase/firestore'
import { useUserStore } from './user'

export const useChatStore = defineStore('chat', {
  state: () => ({
    dm_chats: [],
    gm_chats: [],
    user: useUserStore(),
  }),
  actions: {
    async findChats(id) {
      // code to fetch chats from firestore and categorize them
    }
  }
})

The dm_chats array should be displayed using a v-for loop in PeopleListing component.

Snippet from PeopleWrapper.vue:

<script setup>
import peopleListing from './people-listing.vue'
import { useChatStore } from '../../stores/chat'
import { storeToRefs } from 'pinia';

const chat = useChatStore()
const { dm_chats: chats } = storeToRefs(chat)
</script>

<template>
  <div>
    Direct Chats
    <peopleListing v-for="dm_chat in chats" :dm_chat="dm_chat" />
  </div>
</template>

Similar approach is used for gm_chats in GroupListing. However, the issue arises when updating the arrays does not reflect on screen beyond the initial load from Firestore.

Tried solutions include:

  • Merging both stores into one file
  • Changing Pinia syntax from option to setup
  • Using reactive in the array setup
  • Trying computed instead of storeToRefs in both syntaxes
  • Attempting $subscribe and vue watch methods

Despite various attempts, the issue persists. Any guidance on this matter would be greatly appreciated. Thank you!

Answer №1

Kindly refrain from referring to the store with another store's name, delete this line of code: user: useUserStore(),

Instead, call it within the action using:

async retrieveConversations(id) { const user = userStore() }

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

Position the column content to the right side of the cell using React MUIDataTable

I'm a beginner with MUI and I need assistance aligning the content of a column to the right. Here is my code snippet: <MUIDataTable title={""} data={data || []} columns={realColumns ? realColumns(data, modeMO) : columns(data, modeMO ...

Issue with nested views in Angular UI-Router not displaying properly

The issue I'm facing is that the template <h1>HELLO</h1> is not loading into the nested ui-view in analysis.client.view.html. However, the ui-view in the analysis.client.view.html file is being loaded successfully. I've tried naming t ...

Is there a straightforward method to retrieve all information from Vue.js?

Is there a way to extract all of this data? I've attempted using this._data.forEach but it doesn't seem to be effective. Thank you! data() { return { childData: '', credit: '', company: '', email: ...

Activating checkboxes will enable the button in VueJs

I'm currently exploring methods to enable a button only when both checkboxes are checked. The button is currently disabled, but I need a way to trigger the isActive state when both checkboxes are checked. <div class="form-check mb-1"> ...

Node.js: Configuring keep-alive settings in Express.js

How can I properly implement "keep alive" in an express.js web server? I came across a few examples.. Example 1: var express = require('express'); var app = express(); var server = app.listen(5001); server.on('connection', function(s ...

Why do I continue to encounter the error message saying 'jQuery is not defined'?

As a newcomer to the world of jQuery and Visual Studio Environment, I embarked on the journey of creating a circular navigation menu bar following the instructions provided in this link. Despite my efforts, I encountered a hurdle in the head section where ...

Is there an Angular directive that can replicate a mouseenter event?

Is there a way to simulate a mouseenter event with a directive? I have been searching for a directive that can simulate a mouseenter event, but all I have found so far is one that binds a function to mouse over or karma tests for simulating mouse over. W ...

The JS Uri request is being mishandled

My current challenge involves POSTing to a server using Request JS, specifically with some difficulties related to the path. return await request.get({ method: 'POST', uri: `${domain}/info/test/`, body: bodyAsString, head ...

Interpolating SVG and HTML attributes in Vue.js

Wondering if this is the proper method to interpolate a value into an svg attribute using Vue. It seems to be effective for me, but I can't find any mention of it in their documentation. <svg :stroke-width="`${strokeWidth} px`" > ...

css - the art of centering span text within a rounded button

CodeSandbox: https://codesandbox.io/s/eloquent-haibt-1bnib?file=/src/main.js https://i.sstatic.net/HDtft.png I'm trying to center the dash text within a button, but I'm struggling to find a solution. html <button class="round-butto ...

Creating a Vuejs index page through Node and Express: A step-by-step guide

I am currently developing a small application using Vuejs + Node, but I am facing the challenge of running 2 servers during development: Firstly, my node server: nodemon server.js Secondly, the Vuejs built-in script: npm run dev, which runs webpack-dev-s ...

Is it possible to transfer a massive number of files using node.js?

I need to asynchronously copy a large number of files, about 25000 in total. I am currently using the library found at this link: https://github.com/stephenmathieson/node-cp. Below is the code snippet I am using: for(var i = 0; i < 25000; i++ ...

Is it possible to save edits made to CKEDITOR by clicking a button located outside of the editor?

I am having an issue with inserting HTML code into my CKEDITOR. I have a button on the page that, when clicked, calls: editor.insertElement(link); After inserting the HTML code correctly into the editor, any changes made (such as clicking the 'show ...

What methods can I use to modify strings within JSX?

Within a JSX file, I am faced with the task of manipulating a particular string in a specific manner: Imagine that I have the following string assigned to medical_specialty = "Plastic Surgery" The objective is as follows: medical_specialty.replace(&apos ...

problem with creating a django template script

Hello, I am currently working on a code that is responsible for executing various functions within the template. I have utilized scripts to verify these functions using if-else statements and for loops. However, I am encountering some errors during this pr ...

Updating a React application that was originally built using Node v16 to the latest version of Node, v18,

I have a React project that was originally built on node v16 and now I need to update it to node v18. How can I do this quickly without changing dependencies or causing other issues? When I tried installing the dependencies in node 18, everything seemed f ...

The presence of Bootstrap remains hidden unless space is designated for it

Question about Bootstrap 5.1.3: Is there a way to hide elements on a page using the bootstrap class ".invisible" without allocating space for them? Currently, when the elements are set to visible using the class ".visible", they occupy space on the page ...

Tips on revitalizing a bootstrap wizard

In my JSP file, I am using a Bootstrap wizard. You can see the wizard layout in the following link: The wizard allows me to add employee elements that are stored in a JavaScript array (I also use AngularJS). At the final step of the wizard, there is a su ...

Adjust the div class to align with its content

I am looking to modify the code in order to copy the text of a div to its own class. Currently, the code provided copies text from all sibling div elements, but I specifically want each individual div's text to be its own class. For example, with the ...

Cease the execution of a task in Express.js once the request timeout has been

Suppose we have the following code snippet with a timeout of 5 seconds: router.get('/timeout', async (req, res, next) => { req.setTimeout(5000, () => { res.status(503) res.send() }) while (true) { ...