question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onNavigate lifecycle function

See original GitHub issue

#552 suggested a need for a lifecycle function that gets called when navigation occurs but the destination component is the same as the one we’re already on (since onMount only runs the first time the component is mounted, and the alternative — unmounting components upon navigation — is bad for a variety of reasons).

I propose an onNavigate hook that lives in $app/navigation and looks something like this:

export function onNavigate(fn) {
  let mounted = false;

  const unsubscribe = getStores().page.subscribe(() => {
    if (mounted) fn();
  });

  onMount(() => {
    mounted = true;
    fn();

    return () => {
      unsubscribe();
      mounted = false;
    }
  });
}
<script>
  import { onMount } from 'svelte';
  import { onNavigate } from '$app/navigation';

  onMount(() => {
    // runs once when the page mounts
  });

  onNavigate(() => {
    // runs when the page mounts, and also whenever SvelteKit
    // navigates to a new URL but stays on this component
  });
</script>

As mentioned in https://github.com/sveltejs/kit/issues/404#issuecomment-800630109:

There might be some nuance around ensuring that the callback doesn’t run immediately before the component is unmounted, but you get the gist. This would work in any component (layout or otherwise) like a normal lifecycle function, including components created after the page was initially created (e.g. lazily-loaded stuff).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

17reactions
lowicommented, Mar 26, 2021

We need a hook onBeforeNavigate as well. As referenced by @benmccann in sapper#1047, it’s very useful to be able to cancel navigation when a user is editing something and has forgotten to click “Save”. For example, I show a dialog “You didn’t save your changes. Do you want to stay on the page or lose your changes?”. A basic onNavigate hook doesn’t allow this kind of behavior.

5reactions
PatrickGcommented, Mar 26, 2021

We need a hook onBeforeNavigate as well. As referenced by @benmccann in sapper#1047, it’s very useful to be able to cancel navigation when a user is editing something and has forgotten to click “Save”. For example, I show a dialog “You didn’t save your changes. Do you want to stay on the page or lose your changes?”. A basic onNavigate hook doesn’t allow this kind of behavior.

If this will not be handled by svelte/kit, I will provide a package for it, simliar to this. But I think it would be good, if this were handled by svelte/kit. I would call it beforeNavigate analogous to beforeUpdate.

One question is, should onbeforeunload handled by it, too?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Navigation lifecycle
React Navigation lifecycle events​​​ React Navigation emits events to screen components that subscribe to them. We can listen to focus and blur events...
Read more >
ASP.NET Core Razor component lifecycle | Microsoft Learn
The Razor component processes Razor component lifecycle events in a set of synchronous and asynchronous lifecycle methods.
Read more >
Understanding the Lifecycle of React Navigation to Add ...
Essentially the Home Screen will run the constructor function, render the screen, then componentDidMount() and onwards through the cycle. The React Lifecycle ( ......
Read more >
Navigating Lifecycle Events! - Ionic Blog
In native apps (iOS, Android), views have a well structured lifecycle that allows you to perform actions on key points of their execution....
Read more >
The activity lifecycle | Android Developers
Within the lifecycle callback methods, you can declare how your ... To navigate transitions between stages of the activity lifecycle, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found