Lifecycle Hooks
mount
- 컴포넌트가 만들어질 때 최초 한 번만 호출
- Livewire 컴포넌트는 생성자를 사용하지 않음
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
class UpdateProfile extends Component
{
public $name;
public $email;
public function mount()
{
$this->name = Auth::user()->name;
$this->email = Auth::user()->email;
}
}
boot
use Livewire\Attributes\Locked;
use Livewire\Component;
use App\Models\Post;
class ShowPost extends Component
{
public $postId = 1;
protected Post $post;
public function boot()
{
$this->post = Post::find($this->postId);
}
}
updating
- 컴포넌트의 프로퍼티가 업데이트 될 때 호출
- 클라이언트는 여러 방식으로 프로퍼티를 변경할 수 있지만 대부분
wire:model 을 통해 변경한다
use Exception;
use Livewire\Component;
class ShowPost extends Component
{
public $postId = 1;
public function updating($property, $value)
{
if ($property === 'postId') {
throw new Exception;
}
}
}
updated
use Livewire\Component;
class CreateUser extends Component
{
public $username = '';
public $email = '';
public function updated($property)
{
if ($property === 'username') {
$this->username = strtolower($this->username);
}
}
}
rendering & rendered
render() 메서드 호출 전에 실행 & render() 메서드 호출 후에 실행
use Livewire\Component;
use App\Models\Post;
class ShowPosts extends Component
{
public function render()
{
return view('livewire.show-posts', [
'post' => Post::all(),
])
}
public function rendering($view, $data)
{
}
public function rendered($view, $html)
{
}
}
hydrate & dehydrate
- 최초 렌더 요청 이후 들어오는 후속요청 때마다 호출 & 모든 요청이 종료될 때 호출
use Livewire\Component;
class ShowPost extends Component
{
public $post;
public function mount($title, $content)
{
$this->post = new PostDto([
'title' => $title,
'content' => $content,
]);
}
public function hydrate()
{
$this->post = new PostDto($this->post);
}
public function dehydrate()
{
$this->post = $this->post->toArray();
}
}