Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kris01's avatar

Inertia VUE JS + pinia

Hello guys, I am using pinia on inertia vue js to track the state of a variable. My code is structured like this.

component1 which in this case updates the state of the variable, this is the snippet:

import { useSelectedInvoicesStore } from '../stores/SelectedInvoicesStore'
const selectedInvoicesStore = useSelectedInvoicesStore()

const selectInvoice = (id, invoice_number) => {

    let invoice = {
      id,
      invoice_number
    };
    
    selectedInvoicesStore.addInvoice(invoice);
}

in my component2 I have a watch that IS NOT BEING TRIGGERED on when invoice is added, and that is my problem

import { useSelectedInvoicesStore } from '../stores/SelectedInvoicesStore'
const selectedInvoicesStore = useSelectedInvoicesStore()
watch(
  () => selectedInvoicesStore.selectedInvoices,
  () => {
    console.log('store ', selectedInvoicesStore.selectedInvoices)
  }
);

and this is my SelectedInvoiceStore.js

import { defineStore } from 'pinia'


export const useSelectedInvoicesStore = defineStore('selectedInvoicesStore', {
    state: () => ({ selectedInvoices: [] }),
    actions: {
        async addInvoice(invoice) {
        this.selectedInvoices.push(invoice);
      },
    },
});

I do not understand where is the problem, why is my watch not being triggered in component2, when I add a console.log in the function where I add the invoice the invoice is being added successfully to the store, but the watch is not being triggered.

0 likes
3 replies
richardhulbert's avatar

Hi there

First question is why you need to store the state on the client. This is the very reason for using Interia in the first place. Data is all defined on the server and kept sync behind the scenes

Kris01's avatar

@richardhulbert it is not optimal in this case to make a request each time a person checks a box.

First they check all their boxes and finally we make a request.

3 likes
joseburgon's avatar

@Kris01 I'm also working on a project where I face the same situation. I haven't implemented a global state solution on the client yet but I'm leaning towards it. I usually have a page component from where I send a request to the server after the user has entered several information trough child components, having multiple levels of nesting, which means multiple levels for events to reach out to the page component. So far, how has your experience been using Pinia together with Inertia?

Please or to participate in this conversation.