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.

Access path in other components

See original GitHub issue

Is there any way to access/watch current path in components that are available in all routes for example header. I would like to show different links based on current path.

<Router url="{url}">
<AppHeader>
  <div>
    <Route path="/welcome" component="{Welcome}" />
    <Route path="/login" component="{Login}" />
  </div>
</Router>

<!-- AppHeader -->
<header>
  <nav>
  {#if currentPath != '/login' }
    <Link to="/login">Login</Link>
  {:else}
  <Link to="/welcome">Go Back</Link>
{/if}
  </nav>
</header>

Is there any preferred way to day this?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

20reactions
kysoniccommented, Jun 19, 2019

@iamfrntdv How about the following approach?

import {getContext} from 'svelte';
import {ROUTER} from 'svelte-routing/src/contexts';
const { activeRoute } = getContext(ROUTER);

$: {
    console.log($activeRoute)
 }
5reactions
LaughingBubbacommented, Jan 8, 2020

Thanks @kysonic, your solution works fine for me.

I moved my <navbar> component to be in scope of the <Router>

<Router>
	<NavBar isLoggedIn={isLoggedIn} loggedInUser={loggedInUser} />
	<Route exact path="/">
		<Home isLoggedIn={isLoggedIn} loggedInUser={loggedInUser} />
	</Route>
	<Route path="/panels" component={Panels} />
	<Route path="/users" component={Users} />
	<Route path="/login" component={Login} />

	<Route path="*" component={NotFound} />
</Router>

Then on the <NavBar> component:

<script>
  import { getContext } from 'svelte'
  import { ROUTER } from 'svelte-routing/src/contexts'
  var { activeRoute } = getContext(ROUTER)
   ...
</script>

<main>
    ...
    {#if !isLoggedIn && ($activeRoute ? $activeRoute.uri : '') != '/login'}
      <button type="button" on:click={()=>navigate('/login')}>Login</button>
    {:else if isLoggedIn}
      <span >{loggedInUser.displayName ? loggedInUser.displayName : ''}</span>
      <button type="button" on:click={logout} outline>Logout</button>
    {/if}
   ...
</main>

This way the the login button doesn’t get displayed on the <navbar> when the user is on the \login route

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot access URL path components with react-router
I'm doing this in App.js: <Route path="/discover/:query" component={Discover}/>. Then I'm trying to access the URL parameters in Discover :
Read more >
How To Get Route Path Parameters In Non-Routed Angular ...
Let's call it (and other such components) the non-routed components. As it turns out, getting route path parameter in a non-routed component is...
Read more >
How do I connect to a shared folder on the network?
To map a network drive, open up My Computer and select Tools, Map Network Drive. Select an available drive letter and then enter...
Read more >
File path formats on Windows systems | Microsoft Learn
A standard DOS path can consist of three components: ... directory called hidden. , for example, is impossible to access in any other...
Read more >
Accessing File Path Components (Recap) - Real Python
You learned about .parents that returns paths to all directories contained in the file path, and those are Path objects as well, so...
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