alijumaan / laravel-ecommerce

142 stars 71 forks source link

This is not a proper way of using mount method #4

Closed damnlord closed 3 years ago

damnlord commented 3 years ago

https://github.com/alijumaan/Laravel-Ecommerce/blob/0fd727565de50cd3063262d7738a53a2f539cc8a/app/Http/Livewire/Counter.php#L12

In Livewire components, you use mount() instead of a class constructor __construct() like you may be used to.

You even don't need mount method , if don;t know what mount is I can suggest to run some experiment. you can do it like this: public function render() { $this->cartItems = \Cart::session(auth()->id())->getContent()->toArray(); return view('livewire.counter'); }

protected $listeners = ['cartUpdated' => '$refresh'];

and you will remove mount and onCartUpdat method

It's not a good practice to call mount method from other methods.

or even shorter
protected $listeners = ['cartUpdated' => '$refresh']; public function render() { return view('livewire.counter', ['cartItems' => \Cart::session(auth()->id())->getContent()->toArray()]); }

Mount method will be used just once when the component is loaded, the render method is used every time you refresh or load the component, so my suggestion here to use only render, become you don't have any data that has to be loaded just once and used in the code later or a data that has to be loaded before render like empty model for example for your edit modal or something like that.

alijumaan commented 3 years ago

good note I will change it. Thank you for your review.