Skip to content

Commit

Permalink
Minor update of the Golden rule ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-rubel authored Dec 27, 2022
1 parent 5fc13af commit 031f060
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ Livewire requires a [root element](https://laravel-livewire.com/docs/2.x/trouble
Don't pass large objects to Livewire components!
```

Avoid passing objects to the component's `mount` method or properties if possible. Use primitive types: strings, integers, arrays, etc. That's because Livewire serializes/deserializes your component's payload with each request to the server to share the state between the frontend & backend. If you need to work on objects, you can create them inside a method or computed property, and then return the result of processing as an array or paginated collection if needed.
Avoid passing objects to the component's `mount` method or public properties if possible. Use primitive types: strings, integers, arrays, etc. That's because Livewire serializes/deserializes your component's payload with each request to the server to share the state between the frontend & backend. If you need to work on objects, you can create them inside a method or computed property, and then return the result of the processing.

What to consider a large object?
- Any instance large as Eloquent Model is big enough already for Livewire to slow down the component lifecycle, which may lead to poor performance on live updates. For example, if you have a component that represents the user profile (email and username), it's better to pass these parameters to properties as strings instead of the assignment of the whole model and extracting its attributes in the view.
- Any instance large as Eloquent Model is big enough already for Livewire to slow down the component lifecycle, which may lead to poor performance on live updates. For example, if you have a component that represents the user profile (email and username), it's better to pass these parameters to properties as strings instead of the assignment of the whole model and extracting its attributes in the view. Alternatively, you can fetch the model by using [route model binding](#%EF%B8%8F-use-route-model-binding-to-fetch-the-model) and then keep it as a `protected` property. You'll still be able to access the protected objects in Blade templates by using `$this->yourProperty`.

Note: if you use [full-page components](https://laravel-livewire.com/docs/2.x/rendering-components#page-components), it's recommended to fetch objects in the full-page component itself, and then pass them downstairs to the nested ones as primitive types.

Expand Down

0 comments on commit 031f060

Please sign in to comment.