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

sAnsic's avatar

Livewire 3 @js directive and Laravel component

So we have the following situation (only the essence of the problem)

@php($items = ['1', '2', '3'])

// it's work
// <button wire:click="$dispatch('action', { items: JSON.parse('[\u00221\u0022,\u00222\u0022,\u00223\u0022]') })">...</button>
<button wire:click="$dispatch('action', { items: @js($items) })">...</button>

// it's don't work. 
// Alpine Expression Error: Invalid or unexpected token
// Expression: "@js(['items' => $items])"
// <button wire:click="$dispatch('action', { items: @js($items) })">...</button>
<x-button wire:click="$dispatch('action', { items: @js($items) })">...</x-button>

x-button component (Laravel):

<button {{ $attributes }}>
    {{ $slot }}
</button>

As you can see from the comments, in the case of the tag, the DD directive was a parsed, but not parsed in the component. Does anyone have any ideas or thoughts?

0 likes
3 replies
DoubleClickDesignLtd's avatar

Hi @sansic, I've been using Livewire 2 for about 2 years now and only just started looking at Livewire 3, so I'm gonna be honest and say my suggestion is an educated guess based on what your saying in the thread.

Here why I believe the below does not work.

<x-button wire:click="$dispatch('action', { items: @js($items) })">...</x-button>

I believe it is because your made a Laravel blade component not a Livewire child component and your passing Livewire to it.

When you pass the $attributes across it simply does not know what to do with wire:click etc...They only work in Livewire components.

I'd suggest you make a Livewire child component then add it to your parent view. If you need to pass values to the child from the parent you can do something like this if you need to.

<livewire:child-view-name :$items />

I hope the above helps.

sAnsic's avatar

@DoubleClickDesignLtd, thank you for your time and response. But this is not the problem - wire:click works without parameters. The problem is that when passing parameters through the @js directive, this directive does not unwrap the data

Chingy's avatar

@sAnsic I am having issues with the @js directive as well. I believe it is been deprecated, though i can't find somewhere that states that, nor somewhere where it validates its existence in LW3 docs.

Refrain from using it

Please or to participate in this conversation.