In this episode, we tackle how to properly reset your state when a modal closes—whether that's from pressing Escape, clicking outside the modal, or hitting a "Close" button. Up until now, we've seen that closing a modal doesn't automatically reset things like counters or form fields, unless you refresh the page. We want a smoother experience where every time the modal opens, it's fresh.
To solve this, we introduce event dispatching! Specifically, when the modal hides (closes), we dispatch a hidden
event back to just that component. Listeners can then respond to this by resetting any state, like setting a counter back to zero. You see this in action with multiple modals: closing one only resets its own state, not the state of other open modals—everything stays independent.
We discuss some caveats, such as what happens if you close the modal by clicking outside or using the Escape key, which in our existing setup didn't trigger the reset logic. To fix this, we make sure those interactions also dispatch the hide
event so the state always gets cleaned up. There's a little Livewire/Alpine.js coordination to make it all work, and we show you how to get the current component name dynamically to target the right modal.
By the end of the video, you’ll know how to ensure your modals always clean up after themselves—no leftover state, no surprises—whether you’re incrementing counters or resetting forms. You’ll also pick up some tricks about scoped event handling in component systems. Enjoy!