Call Livewire method from native JS

What seems to be the problem:

I’m trying to work out how to trigger my “next()” PHP method from my Livewire component from native JS, specifically on a keypress. How can I achieve this?

Steps to Reproduce:

    window.onkeydown = function (e) {
        if (e.keyCode === RIGHT_ARROW) {
            next();
        }
    };
1 Like
window.onkeydown = function (e) {
    if (e.keyCode === RIGHT_ARROW) {
        window.livewire.emit('triggerNext');
    }
};

In a component:

protected $listeners = ['triggerNext' => 'next'];

public function next()
{
    //do some next stuff
}

Untested, check for typos.