8
votes

I have 2 livewire components, 1st only displays the session variable of the cart and the 2nd one is just to add items in cart (a very raw form with sku, title, price and qty).

<livewire:shoppingcart :cart="$CartId">

<livewire:order-add-product-form :orderAddProductCartId="$CartId">

Both the components are working fine in itself. But when i add item from the second component, it does update the cart session variable, but the view never updates. I have to refresh the page to see the session variable in the cart view.

Is it possible to connect both the components together. So, when i add item in cart from one component, it automatically updates the view of other component?

Thanks

1

1 Answers

19
votes

From Second component after you add a new product you can emit a event like:

$this->emit('cart:update');

See the docs: Livewire Events

Now you can listen the event from first component and use special action of livewire called $refresh

See the docs: Special Actions

protected $listeners = [
    'cart:update' => '$refresh',
];

I think that solves your problem.