This episode is for members only

Sign up to access "Building Reactive Realtime Applications with Livewire" right now.

Get started
Already a member? Sign in to continue
05. Reactivity when adding a new post


Okay, so not only are we gonna be creating the form
to push a new post in this episode, we are gonna talk about the reactivity of this because it's basically just not gonna work. Okay, let's get started.
We'll build the form out and do everything that we would normally do. And then we'll look at some of the issues. So let's go ahead and make out a create post form in here.
We're just gonna create an entirely new component for this to keep it separate. And let's head over to our dashboard and just put this above the post index here.
So Livewire create post. And there we go. So we'll go ahead and open up our create post form and let's just start to build this out.
So we'll make this a form and let's just reuse some of the elements that we get with our starter kit here. So I'm gonna create a label out for the body
and say post body, but I'm gonna hide this with the SR only class in Livewire. And then down here,
we'll just start to use some of the blade components that already come with this starter kit. So we'll have a text area input, which doesn't exist at the moment.
We do have an input, but it's gonna work in exactly the same way. So we'll switch this over and we'll set a class of width full to this
and we'll set a height of 28 and a padding of four. Now, if we just go over to our textinput.blade.php, I'm basically just gonna copy this, create our new component
and it's gonna pretty much behave exactly the same, but obviously it's going to be a text area instead. So we'll create that in there. We'll swap this out for a text area
and of course, end the text area there. But other than that, it's just gonna work in exactly the same way. Okay, so we've got our text area on the page.
Let's have a look. There it is. So we can obviously start typing and just down here,
we'll have our input errors because of course we need to validate this. So that's just X input error. And then the messages we get through into here
are just the errors that we get through. Now, in this case, this form field is just gonna be called body. So let's say errors get body.
And to be honest, that's pretty much it. Let's go ahead and hook up the text area to a body property
that we're gonna have with inside this component. And while we're here, let's add a placeholder and just say,
what do you want to say? Okay, so we'll add our submit button down here, which is just gonna be our X primary button and the off and we'll just say post.
And for this outer form, let's set a space on the Y axis of two. And then why don't we just hook this up now? We don't have any methods
or anything like this here, but let's say wire submit and we'll just say create or submit. What if we want to say?
So that's our form. We'll separate this out in a second, but we have everything on here that we need. Okay, so over in our create post component itself,
we know that we need to store a body in here and we can go ahead and validate this. So we can use our rule in here to say this is required. We'll just keep it like that for now.
We can customize this as well. So we could set a specific message in here. You need a post body, whatever you want us to say,
and then we'll create out that create method and we'll go ahead and validate and then we'll create the post and then we will push it to our list.
Well, in this case, we're gonna be broadcasting or dispatching an event within LiveWire so we can read it from our other component,
but that should be pretty much it. So let's click post and everything's working. Okay, let's tidy this up before we go any further. So I'm just gonna set a space Y of eight in here
and I think that's okay. We can leave it like that for now. Okay, so let's go and create the post out, which is pretty straightforward.
And then we'll look at really importantly how to get it into our list. So let's go ahead and create a post out in here. So we need to do this via the user posts relationship
and we'll create this and we're just gonna say this only body. So just put that in there and then we wanna go ahead and reset the body.
So this reset body and that's it. So that will actually create the post for us. So I'm just gonna write something in here and hit post. Obviously nothing happens at the moment
because we're not doing anything with this. This is a completely separate component. So remember this is not re-rendering when we do that. Obviously when I refresh the page, it will come in.
Now it's not coming in at the moment. The reason for that is just the ordering of the stuff that we've got in the database because we can see that this has been created
because we created these, the dates are overlapping. So for now, I'm just gonna get rid of everything out of the database and we'll just check that this works nicely.
Okay, so we've come across an issue here where we can't render out the first thing. So it's probably good to fix this up now if we don't have any posts in the database.
So let's go over to our post index here and just fix this up. So let's go and create an if statement out here and say count chunks and end that.
Well, we could do this for this entire thing actually. End that there and we shouldn't see that error, great. Okay, so now when I type something, hit post and refresh, we get that first one in there.
Okay, so we want this to push automatically to this list. So the problem that we've got here is if we go and say this dispatch and then say post created,
and then we give the post ID because remember we only need the post ID to push into the first batch that we've got. This will push it to it
but we won't get the reactivity that we need. So we'll see how to solve that in a second. Okay, so if we go over, we've dispatched this post created event with the ID,
we can pick that up now in our post index. So let's go over to our post index component and let's listen for this and then do something. So let's do this down here
and we will call this prepend post, that kind of makes sense. And we know that we get the post ID in with that event and we can listen to this in here with the on attribute.
So we can say when we get that post created event, invoke this and then do something with that post ID. Just to demonstrate that this does come through, let's go over and type something in,
hit post and there we go from our main index component, that's now dumping out the post ID. Now this post, because we're not just dealing with a normal list of posts
and we don't wanna refresh the entire list of posts, that's really, really important. We need to push this ID onto the first chunk. So the first thing that we need to do is be really careful
if we don't have any chunks. So if we don't have any records, we need to create the first chunk before we push to it. So the first thing that we're gonna do
is check if the chunks array is empty. And if it is, we're gonna kind of initialize it with a default empty array in here.
Now down here, what we can do is go and say this chunks zero. So the first chunk that we have, cause we always wanna push this to the first chunk
that we're dealing with. And we're gonna set that to the post ID cause that always needs to be at the top within an array. And then we're just gonna spread out
the existing IDs from within there. So we're just gonna say this chunks and zero. So basically build up a new array with the existing chunks for the first chunk
or the existing IDs for the first chunk, but then put the post ID on top of all of that. So now that we've done this, we would think this would work.
So let's go over and just write another message in here. I'll put something slightly different. Hit post and you can see nothing happens. Now the code here is completely valid
and it should be working. Why isn't this working? Well, if we cast our mind back to when we looked at the basics of reactivity
a little bit earlier on in the course, we know that when we are over in our post index to start with, we're passing down the IDs into this post chunk component.
Now, when we modify the chunks, which we've just done by pushing that new post to it, this is not reactive because we haven't told this to be reactive from our post chunk component.
So this is a really easy fix, but this is the kind of lesson here. With reactive, when we add that attribute, it will work, but then we'll have a knock on effect
when we start to do more infinite scrolling or whatever else you're doing in your application. So we need to be really careful with this. I'm gonna add this now and then later on,
we are gonna refactor this once we see the issue. So let's go over to our post chunk component and as easy as we saw from the small example earlier, we are going to set this to reactive.
Now I'm gonna say this again, because this is so important. This is a solution, but we need to be really careful where we use reactive. So we don't end up with too many re-renders
and that's the whole point of the course. So we'll be changing that later, but let's get this working now. And then of course we can always refactor it,
it's only code. Okay, so I'm gonna go ahead and type another message in here, hit post. Now that we have that reactive property
and that array has been updated with a new ID, the component here has been re-rendered. Now let's just take a look at this in our network tab, just so we can see what's going on.
So let's go ahead and type another message in here and hit post. And this one, of course, this request was to actually post that data through.
And this is where the reactivity comes in and actually gives us that new data back with the new thing in here. So you can see that we've got a snapshot here
and we've got a snapshot here. So we've got two in here. We saw that example from earlier where the more reactive our components are
within each other, the more markup we're gonna get through back from Livewire to rehydrate what we see on the page. But that's pretty much it.
So we've got this working now, we've created a separate component in here, dispatched an event, which has created an additional network request.
We'll talk about the trade-offs later. And then we have, of course, set that to reactive. So this actually updates. So not too bad at the moment,
but let's continue on, particularly with our real-time stuff and see how we get on with just adding that reactive attribute.
12 episodes1 hr 43 mins


Livewire can react and re-render anywhere you need it to. But what if we have a infinite scrolling timeline of user posted content with updates in realtime? With added reactivity, we need to be a bit more careful about performance.

Let’s build this together, making sure we keep things running smoothly along the way.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.