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.

Unable to access current route name in component

See original GitHub issue

Since this.props.url is deprecated, we can no longer access the route’s name through the props of the component.

If you append the name of the current route through server.js, it will stop working during client-side routing.

Inspecting the Router object shows that the ‘name’ of the route is not being appended anywhere, hence the withRouter solution described in the deprecated page fails too.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
shift-keshav-pudaruthcommented, Jun 11, 2018

Solution

We extend our _app.js file and inject the url props to allow it to work as before:

import React from 'react';
import Routes from '../routes';

class MyApp extends App {
    static async getInitialProps ({ Component, ctx }) {
        let pageProps = {};

        if (Component.getInitialProps) {
            pageProps = await Component.getInitialProps(ctx);
            pageProps.url = Routes.match(ctx.asPath);
        }

        return {pageProps}
    }

    render () {
        const {Component, pageProps} = this.props;
        return (
            <Container>
                        <Component {...pageProps} />
            </Container>
        )
    }
}

Then access the url props directly in your pages. Name of the route will be found at this.props.url.route.name.

Hopefully, this solution will solve a few headaches. Been stuck on this one for the past hour.

4reactions
fridayscommented, Sep 2, 2018

Totally! I’m short on time at the moment but review asap. Thanks for contributing!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get the current route name into the component - Stack Overflow
I am trying to access the router information from the component because I need to apply a classname depending on the route. Here's...
Read more >
How to get current route URL in Angular
Steps to get current route URL in Angular. 1. Import Router,NavigationEnd from angular/router and inject in the constructor. 2.
Read more >
Route - Angular
A configuration object that defines a single route. A set of routes are collected in a Routes array to define a Router configuration....
Read more >
Routing - Laravel - The PHP Framework For Web Artisans
Middleware; Controllers; Subdomain Routing; Route Prefixes; Route Name Prefixes ... Form Method Spoofing; Accessing The Current Route; Cross-Origin Resource ...
Read more >
Navigation Guards | Vue Router
As the name suggests, the navigation guards provided by Vue router are ... these hooks do not get a next function and cannot...
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