In this episode, we start with a brand new Laravel project set up with Laravel Breeze and Livewire installed. We make sure to remove any Alpine.js leftovers, just to avoid conflicts, and confirm our database is migrated with the default tables. Then, logged in and on our dashboard, we dive into the basics of Livewire reactivity.
To show how reactivity works, we set up two Livewire components: a SumList
component that manages an array (our 'things'), and a SumListCount
component that simply counts how many items are in the array. We follow a simple example from the Livewire docs—all for the sake of demonstrating how component state by default is isolated and what that means for interactive UIs.
We wire up the list so you can add new "things" (just random strings), and display them as you click a button. As we play around with adding items, we peek at the network tab to see what Livewire sends back and forth on every button click. Then, we nest our counter component inside for displaying the count, but quickly notice the count doesn't update along with the list! That's because nested components in Livewire aren't reactive by default—there's actually a good performance reason for this, so not every change ripples through every child component.
To make the count update along with the list, we introduce Livewire's reactive
attribute to the child component and show how it solves the problem. We do a quick side note on the danger of abusing this feature—if you make everything reactive, you're going to blow up your requests and slow down your app, especially once you start dealing with things like infinite scrolling and large lists.
By the end of the episode, you should have a good feel for what "reactivity" means in Livewire, where it comes in handy, and why you shouldn't just throw it everywhere. Next up, we'll start applying these concepts to a bigger app, and see how to keep things snappy as our UI gets more complex!