In the last video we ran into some big issues around applying them boys.
So in this video we're going to figure out a way to work around all this stuff.
We're going to first begin by deleting this little kind of reactor we did right here and then I'm going
to uncomment our original code.
OK.
So here's what we really need to do.
We need to define a function outside of our action creator.
That's going to actually make the request and then dispatch or action and we're going to memorize it
outside of the action creator so that it only gets memorized exactly one time and it's not going to
be remembered sized every time that we call our action creator effects user.
So underneath my action Creator I'm going to to clear a new variable called underscore Thach user.
I'm putting in the underscore right here to indicate that this is a private function so to speak and
that other engineers should not attempt to call this function unless they really know what they are
doing.
It's going to say underscore fecche user.
And then we're going to set up an aero function here and this arrow function right here is going to
be what actually makes a request and then dispatches an action.
So I'm going to cut the request and the dispatch and put them in the new function right here and then
I'm going to make sure that I mean Elize this thing by wrapping it with a memorized call so opening
parentheses then why is closing parentheses over here.
Now we can make sure that we call the memorized version of Fettes user
inside of our action creator.
Like so.
All right.
Now this almost works.
There's just one little issue.
Are fecche user function underscore needs to get the idea that we want to fetch.
And it also needs a reference to the dispatch function.
So we need to somehow get these arguments right here into fecche user so we should have to do here is
make sure that we call ID or fetch a user with the given ID and we'll pass and dispatch as well.
So now our memorized function right here will receive the ID and dispatch as arguments.
OK so that should be it.
I think that we should be able to test this thing.
Now one last quick thing that I just noticed I apologize.
We no longer have the Awake word on this inner function right here so we can remove the async and we
actually need to move that async to the arrow function down here like so because the botia down here
is the one that actually contains a weight now.
OK.
So this should be good.
So let's save this now a flip back over.
We can refresh the page and now will see that we only attempt to fetch each user exactly on time because
we have now correctly minimalized the Fettes user ID or underscore fecche users function.
So this thing can only be called exactly one time and we only memorize it one time as well as opposed
to memorizing or redeclare in the function and memorizing it every time that we call our action creator.
Now there is one last little refactor that you could do inside of your totally optional.
If you do this refactor things start to look really crazy.
But nonetheless it is optional.
So if we wanted to we could remove this curly brace right here we could remove the curly brace and semi-colon
right there.
And then we could add new line that entire expression.
And so we would end up with something that looks like this right here.
So now we've got a function that returns a function that calls underscore fecche user with the ID and
dispatch.
So like I said this is a solution that involves just changing a couple lines of code but it has some
really crazy syntax like when you look at this thing right here in the future you're probably going
to have to really really think about it for a little bit and try to understand what's going on.
So I don't know if this is the best solution.
I would just say that this is a solution.
Now the one side effect to the solution that I want to mention is that if you ever want to re fetch
a user for some reason like if you have made a change to a user or if you know that the user data has
been updated on your API and you want to reattach it unfortunately you would not be able to do it again
using this action creator with this solution.
You can only fetch each user exactly one time inside of your application.
So if you wanted to be able to reset your user you would have to essentially like declare a another
action creator that has the same logic but just does not apply that memoization step.
So like I said maybe not the best solution but it is a solution that fixes our problem.
All right.
Let's take a pause right here in the next video.
I'm going to show you a second solution to this problem.
That's a little bit more universal I suppose.
Or it's going to work out in more cases a little bit better because we are not having to do this memoization
step.
However it's going to involve changing a lot more code than what we had to do for this right here.
So let's take a quick pause and we'll take a look at that solution in the next video.