All right so let's figure out how we can disable location tracking.
Anytime a user navigates away from track create Believe it or not.
This is actually going to be kind of surprisingly challenging.
I know that might sound surprising but it really is can be kind of difficult.
Now the first thing I want to do is just give you a quick reminder on how we can detect whenever a user
navigates between different screens.
So as a quick reminder inside of a track create screen one way that we can detect navigation changes
from react navigation right here we can import navigation events.
Then inside of our ASX we can put something inside of here called like navigation events and we can
throw in on will blur.
Remember that's a prop that receives a callback and this callback right here here will be invoked anytime
a user starts to navigate away from the screen.
So inside of here I could put like a console log of leaving and if I save this I can go to track create
go back to track list and then I'll see a console log inside my terminal.
Right there.
We also saw that we can attach an event listener directly to navigation events.
Now I want to show you one third way in which we can detect whenever a user navigates away.
And this third way even though it might seem like really over the top to have three different ways of
detecting navigation.
This third way is actually going to be very helpful for what we're going to eventually have to do.
So I'm going to remove the navigation events component right there and from react navigation I'm going
to update that import from navigation events too with navigation focus.
So this is a higher order component.
It's essentially a function that we pass a component into like a track create screen right here when
we add in with navigation focus our component is going to receive a new prop in this new prop is going
to tell us whether or not this particular component is currently focused or actually visible on the
screen.
The prop that we're going to receive in here is called is focused so going to d structure that out of
my props object like so I'm also gonna make sure that I use the with navigation focused function so
down the very bottom.
I'm going to find where I export track create screen and I'll call with navigation focus and pass in
my component.
Now if I save this actually really quickly it's do a console log of is focused inside here so we actually
know what we're doing.
There's a council log.
Now we can save this and I'll navigate to try to create once I navigate there I should be able to check
my journal and I'll see true which means yes this screen is focused if I can navigate away by going
back to my track list it will update and say false instead.
So we've now got a very easy way to detect whether or not we should be doing some location tracking.
We've got the.
Is focused flag.
So that's kind of part one here.
Now it might seem like that's kind of all we need to start up location tracking you might think.
OK that's great.
Now we can just like wrap our hook right here with is focused or something like that.
Well unfortunately that's not going to be quite enough.
That's not going to work.
And the reason for it is that when we start up our location tracking inside of our hook so going to
go to use location really quickly here is use location when we start up this tracking with watch position
async that's essentially a background process of sorts.
As soon as we call that that's pretty much it we're now going to start tracking the user's location
and there's a very singular and very specific way that we can turn off that tracking the way in which
we disable tracking whenever we call watch position a sink.
This is going to eventually give us back a value called subscriber and on this value right here is a
function called remove remove or the remove function on there is what is going to actually make sure
that we stop tracking the user's location.
So while that's pretty much it we need to kind of combine these two ideas into one.
So take the fact of whether or not a screen is focused get that information into our hook and then use
that to decide whether or not to turn off our location tracking through that subscriber thing right
there.
Let's take a quick pause right here and figure out how to handle that in the next video.