-
Notifications
You must be signed in to change notification settings - Fork 224
/
lifecycle-hooks.blade.php
executable file
·135 lines (112 loc) · 4.78 KB
/
lifecycle-hooks.blade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
* [Class Hooks](#class-hooks)
* [Javascript Hooks](#js-hooks)
## Class Hooks {#class-hooks}
Each Livewire component undergoes a lifecycle. Lifecycle hooks allow you to run code at any part of the component's lifecyle, or before specific properties are updated.
@component('components.table')
Hooks | Description
--- | ---
boot | Runs on every request, immediately after the component is instantiated, but before any other lifecycle methods are called
booted | Runs on every request, after the component is mounted or hydrated, but before any update methods are called
mount | Runs once, immediately after the component is instantiated, but before `render()` is called. This is only called once on initial page load and never called again, even on component refreshes
hydrate | Runs on every subsequent request, after the component is hydrated, but before an action is performed, or `render()` is called
hydrateFoo | Runs after a property called `$foo` is hydrated
dehydrate | Runs on every subsequent request, before the component is dehydrated, but after `render()` is called
dehydrateFoo | Runs before a property called `$foo` is dehydrated
updating | Runs before any update to the Livewire component's data (Using `wire:model`, not directly inside PHP)
updated | Runs after any update to the Livewire component's data (Using `wire:model`, not directly inside PHP)
updatingFoo | Runs before a property called `$foo` is updated. Array properties have an additional `$key` argument passed to this function to specify changing element inside array, like `updatingArray($value, $key)`
updatedFoo | Runs after a property called `$foo` is updated. Array properties have additional `$key` argument as above
updatingFooBar | Runs before updating a nested property `bar` on the `$foo` property or a multiword property such as `$fooBar` or `$foo_bar`
updatedFooBar | Runs after updating a nested property `bar` on the `$foo` property or a multiword property such as `$fooBar` or `$foo_bar`
@endcomponent
@component('components.warning')
Please note that mutating a property directly inside a Livewire component class doesn't trigger any of the updating/updated hooks.
@endcomponent
@component('components.code', ['lang' => 'php'])
class HelloWorld extends Component
{
public $foo;
public function boot()
{
//
}
public function booted()
{
//
}
public function mount()
{
//
}
public function hydrateFoo($value)
{
//
}
public function dehydrateFoo($value)
{
//
}
public function hydrate()
{
//
}
public function dehydrate()
{
//
}
public function updating($name, $value)
{
//
}
public function updated($name, $value)
{
//
}
public function updatingFoo($value)
{
//
}
public function updatedFoo($value)
{
//
}
public function updatingFooBar($value)
{
//
}
public function updatedFooBar($value)
{
//
}
}
@endcomponent
## Javascript Hooks {#js-hooks}
Livewire gives you the opportunity to execute javascript during certain events.
@component('components.table')
Hooks | Description
--- | ---
component.initialized | Called when a component has been initialized on the page by Livewire
element.initialized | Called when Livewire initializes an individual element
element.updating | Called before Livewire updates an element during its DOM-diffing cycle after a network roundtrip
element.updated | Called after Livewire updates an element during its DOM-diffing cycle after a network roundtrip
element.removed | Called after Livewire removes an element during its DOM-diffing cycle
message.sent | Called when a Livewire update triggers a message sent to the server via AJAX
message.failed | Called if the message send fails for some reason
message.received | Called when a message has finished its roudtrip, but before Livewire updates the DOM
message.processed | Called after Livewire processes all side effects (including DOM-diffing) from a message
@endcomponent
@component('components.code', ['lang' => 'js'])
<script>
document.addEventListener("DOMContentLoaded", () => {
Livewire.hook('component.initialized', (component) => {})
Livewire.hook('element.initialized', (el, component) => {})
Livewire.hook('element.updating', (fromEl, toEl, component) => {})
Livewire.hook('element.updated', (el, component) => {})
Livewire.hook('element.removed', (el, component) => {})
Livewire.hook('message.sent', (message, component) => {})
Livewire.hook('message.failed', (message, component) => {})
Livewire.hook('message.received', (message, component) => {})
Livewire.hook('message.processed', (message, component) => {})
});
</script>
@endcomponent