0

Problem description:

In my RecipeList.php I am showing the list of recipes:


@forelse($recipes as $recipe)
    <x-recipe-card :recipe="$recipe"/>
@empty
    <p>error</p>
@endforelse

Inside <x-recipe-card :recipe="$recipe"/> I have another livewire component:

<livewire:like-dislike :recipe="$recipe"/>

Upon the initial page load, the like buttons are displayed correctly on all cards. However, as soon as I change the sorting ($sort) within the same parent component, RecipeList, the buttons only remain on the first recipe, while they disappear from all subsequent ones.

Sequence Of Steps:

  1. Load recipe list page (pagination, default sorting "popularity").
  2. Like/Dislike buttons are visible on each recipe card.
  3. Select "newest" or "oldest" in the dropdown sorting list.
  4. Problem: The first recipe still has buttons, but the rest have disappeared.

What i tried:

I added wire:key, but it didn't help, on the contrary, it even caused an error in the logic of alpine.js inside x-recipe-card

@forelse($recipes as $recipe)
    <div wire:key="{{ $recipe->id }}">
        <x-recipe-card :recipe="$recipe"/>
    </div>
@empty
    <p>error</p>
@endforelse

Conclusion:

How can i make the parent component RecipeList.php be able to track the child LikeDislike.php provided that it is in the blade component

1
  • 1
    Could you try @livewire('like-dislike', ['recipe' => $recipe], key($recipe->id))? I believe it's something to do with how livewire handles re-rendering. Commented May 19 at 8:41

1 Answer 1

0
I found a solution

1. Set wire:key inside blade loop:


@forelse($recipes as $recipe)
<div wire:key="{{ $recipe->id }}">
    <x-recipe-card :recipe="$recipe"/>
</div>
@empty
    <p>error</p>
@endforelse

2. Set :key for livewire component in x-recipe-card:
<livewire:like-dislike :recipe="$recipe" :key="$recipe->id"/>
Sign up to request clarification or add additional context in comments.

Comments

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.