7

Using Laravel Livewire, I have a parent and a (repeating) child. The child blade has a call to childMethod() through wire:click="childMethod()".

The problem is that parent->childMethod() is called while I wanted child->childMethod() to be called.

The parent component

class StatementsTable extends Component // parent
{
    public function render()
    {
        return view('livewire.statements-table', [
            'statements' => Statement::limit(10)->get()
        ]);
    }
}

The parent statements-table.blade

<table class="table">
    @foreach($statements as $statement)
        @livewire('statement-line', ['statement' => $statement], key($statement->id))
    @endforeach
</table>

The child component:

class StatementLine extends Component
{
    public $statement;
    public $calls = 0;

    public function childMethod()
    {
        $this->calls += 1;
    }

    public function mount($statement): void
    {
        $this->statement = $statement;
    }

    public function render()
    {
        return view('livewire.statement-line');
    }
}

The child statement-line.blade

{{-- dd(get_defined_vars()) --}}
<tr>
    <td>{{$statement->name}}</td>
    <td>{{$statement->date}}</td>
    <td>{{$calls}}</td>
    <td><button wire:click="childMethod">Plus</button></td>
</tr>

Why do I get

Livewire\Exceptions\MethodNotFoundException
Unable to call component method. Public method [childMethod] not found on component: [statements-table]
1
  • @PaulH I am encountering this issue as well. Did you find a solution? Commented Feb 20, 2021 at 8:11

6 Answers 6

8

Had the same problem. The solution was:

Make sure the child's view has ONE root element as indicated in Livewire Docs.

Sign up to request clarification or add additional context in comments.

1 Comment

Altough this is very important indeed, that is not the reason of the issue. The single root element of the parent is <table> and the single root element of the child is <tr>
2

If you are using livewire version 3, the 'emit' method has been renamed to 'dispatch'. (Likewise emitTo() and emitSelf() are now dispatchTo() and dispatchSelf()).

Look at the documentation: https://livewire.laravel.com/docs/upgrading#events.

1 Comment

This should not be an answer, but an comment, since this is not an answer to the original question.
1

In Livewire 3 function emit() is renamed as function dispatch()

2 Comments

Hi, your answer is a duplicate:. Please read how to ask
This should not be an answer, but an comment, since this is not an answer to the original question
0

You can scope your callbacks in livewire have a look at the documentation available here https://laravel-livewire.com/docs/2.x/events#scoping-events

For your case you should scope to self like this

<button wire:click="$emitSelf('childMethod')">

Comments

0

for passing parameters (string) in a function make sure you are using single '' quotes , note double qoutes "". like :

wire:click="activeTab('product')"

Comments

0

To resolve the issue, ensure that only one element serves as the root of your component.

My error was setting styles as another root element.

This won't work:

<style>
    .example {
        display: flex
    }
</style>

<div>
    <div wire:click="exampleMethod" class="example">EXAMPLE</div>
</div>

To resolve the issue, it is necessary to relocate all the elements to the root.

This will work:

<div>
    <style>
        .example {
            display: flex
        }
    </style>
    <div wire:click="exampleMethod" class="example">EXAMPLE</div>
</div>

Consequently, the issue has been resolved.

1 Comment

One element as root indeed, but that is already the OK in the OP. This tip does not resolve the issue in the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.