I make a draft implementation for my reusable input component.
The code below obviously throws an error.
Question is how to pass the $event
back to register blade to get or log the value of the input?
register.blade.php
<div>
<x-input onChange="(value) => {console.log('value', value)}"></x-input>
<div/>
input.blade.php
@props(['onChange' => 'null'])
<input x-on:change="{{ $onChange($event) }}">
2
Answers
I learned we could just achieve this through Alpine.Js dispatch. I don't need to pass
onClick
props via Laravel component. I just simply use dispatch to listen the event (x-on
).What I like in this implementation is that,
register.blade.php
input.blade.php
A few things here.
First off, your markup is wrong. You have a the closing slash at the wrong end of the closing div. Should be
</div>
not<div/>
.Then you’re using
x-on
withoutx-data
. Alpine only picks up components with the x-data attribute.Finally, events propagate automatically, so you could just listen on the parent instead: