Skip to content

Commit

Permalink
Minor update on the Golden rule ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-rubel committed Jan 12, 2023
1 parent 031f060 commit 27ba67e
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 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.
Avoid passing objects to the component's 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. 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`.
- 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 retrieve 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`, but remember that protected properties should be re-hydrated manually on subsequent requests.

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 27ba67e

Please sign in to comment.