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

jbeduya's avatar

Placing multiple instance of Alpine.data results in an error

I have this very simple blade component:

credits.blade.php

<span x-data="Credits" {{ $attributes }}>
   <span x-text="balance"></span>
</span>

And also this Alpine data directive.

credits.js

export default {
    _balance: 0,

    get balance() {
        return new Intl.NumberFormat('en-US', {
            style: 'decimal',
            maximumFractionDigits: 2,
        }).format(this._balance);
    },

    set balance(value) {
        this._balance = value;
    },
}

app.js

import Credits from './credits.js';

document.addEventListener('alpine:init', () => {
    Alpine.data('Credits', () => (Credits));
});

If I place only one instance of <x-credits />, it works fine. If more, I get this error:

livewire.js?id=5d8beb2e:1174 Uncaught TypeError: Cannot redefine property: $nextTick
    at Function.defineProperty (<anonymous>)
    at livewire.js?id=5d8beb2e:1174:14
    at Array.forEach (<anonymous>)
    at injectMagics (livewire.js?id=5d8beb2e:1162:28)
    at Function.<anonymous> (livewire.js?id=5d8beb2e:3467:5)
    at flushHandlers (livewire.js?id=5d8beb2e:1347:48)
    at stopDeferring (livewire.js?id=5d8beb2e:1352:7)
    at deferHandlingDirectives (livewire.js?id=5d8beb2e:1355:5)
    at initTree (livewire.js?id=5d8beb2e:868:5)
    at livewire.js?id=5d8beb2e:823:7

I'm not sure what's going on. This is not even placed in a livewire component, thought I am using livewire in some other parts of the page.

Does anybody have experienced this issue?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like the issue you're encountering is related to the way Alpine.js initializes components and how Livewire interacts with the DOM. When you use Alpine.data to define a component, it's important to ensure that each instance is unique and doesn't conflict with others, especially when Livewire is also manipulating the DOM.

Here's a potential solution to ensure that each instance of your Alpine.js component is unique:

import Credits from './credits.js';

document.addEventListener('alpine:init', () => {
    Alpine.data('Credits', () => ({ ...Credits }));
});

By using the spread operator (...), you're creating a fresh copy of the Credits object for each component instance. This should prevent the error you're seeing because now each instance has its own unique state and methods, avoiding conflicts with Livewire's reactivity system.

Additionally, make sure that your Credits object in credits.js is designed to be used as a factory function. Since you're spreading the object, it should not rely on any shared state that could cause conflicts between instances.

If you continue to experience issues, you might want to check if there are any other scripts or components on the page that could be causing conflicts with Alpine.js or Livewire. It's also a good practice to ensure that you're using the latest versions of both Alpine.js and Livewire, as they may contain fixes for such issues.

1 like

Please or to participate in this conversation.