Daniel Campos
Daniel Campos

Reputation: 85

Passing Alpine JS value to Livewire component

I was wondering if there's a way to retrieve an Alpine JS variable and passing it to a Livewire component. Something like:

<template x-for="(something, index) in array" :key="something">
   @livewire("component", ["variable" => something])
</template>

Upvotes: 0

Views: 6728

Answers (1)

kenean50
kenean50

Reputation: 588

You can use the Livewire @entangle() feature to share property state between alpine and Livewire.

Define your alpine variable like below:

<div x-data="{ something: @entangle('something') }">
    
</div>

And in your livewire component like this

public $something = null;

Now whenever you change your component variable value it will be updated on the alpine variable and vice versa

Upvotes: 4

Related Questions