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.

Adding hooks with multiple routes

See original GitHub issue

Hello. Fantastic work with Navigo! I am able to get hooks working when adding a route. This may be a very simple matter of understanding Javascript, but I am unable to get the hooks working with multiple routes as in the Demo Basic code.

Is this possible? If so, where would I place the hooks objects when working with multiple routes? Thank you.

Hooks when adding a single route:

router.on(
	"home",
	function () {
		console.log("home");
	},
	{
		before: function (done, params) {
			console.log("before");
			done();
		}
	}
);

Hooks when adding multiple routes at once?

router.on({
	'home': function () {
		console.log("home")
	},
	'page1': function () {
		console.log("page1");		
	}
});

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
krachimcommented, Nov 2, 2017

Hi.

Thanks for your hard work. I rly like this router. Seeing this by searching for another Problem and i think i got an solution? See the folowing example:

router.on({
  about: {
    as: 'about',
    uses: _ => {
      document.getElementById('main').innerHTML = About
    },
    hooks: {
      before: function(done, params) {
        console.log('before', router)
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = 'active'
        done()
      },
      leave: _ => {
        console.log('leave', router)
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = ''
      }
    }
  },
  astronaut: {
    as: 'astronaut',
    uses: _ => {
      document.getElementById('main').innerHTML = Astronaut
    },
    hooks: {
      before: _ => {
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = 'active'
        done()
      },
      leave: _ => {
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = ''
      }
    }
  },
  second: {
    as: 'second',
    uses: _ => {
      document.getElementById('main').innerHTML = Errorpage
    },
    hooks: {
      before: _ => {
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = 'active'
        done()
      },
      leave: _ => {
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = ''
      }
    }
  }
})

by the way, with this snippet i set a css class which underlines the current link. Maybe someone can use this snippet.

That could be done much more efficient, but anyway … you can see the syntax 😃

0reactions
benjwarnercommented, Jun 12, 2019

Extending on solution given by @op3d

Below is the same solution code, but with the params and queryString available:


router.on({
  about: {
    as: 'about',
    uses: function (params, queryString) {
      document.getElementById('main').innerHTML = About
    },
    hooks: {
      before: function(done, params) {
        console.log('before', router)
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = 'active'
        done()
      },
      leave: _ => {
        console.log('leave', router)
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = ''
      }
    }
  },
  astronaut: {
    as: 'astronaut',
    uses: function (params, queryString) {
      document.getElementById('main').innerHTML = Astronaut
    },
    hooks: {
      before: _ => {
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = 'active'
        done()
      },
      leave: _ => {
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = ''
      }
    }
  },
  second: {
    as: 'second',
    uses: function (params, queryString) {
      document.getElementById('main').innerHTML = Errorpage
    },
    hooks: {
      before: _ => {
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = 'active'
        done()
      },
      leave: _ => {
        document.querySelector('a[href=' + router.lastRouteResolved().name + ']').className = ''
      }
    }
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use react context hook with multiple routes?
I'm working on a Chat Application and i wanted to store the information of the logged user so I used useContext hook for...
Read more >
How React Hooks can replace React Router - LogRocket Blog
Looking for an alternative form of routing in your React projects? Read more to find out how React Hooks can replace React Router....
Read more >
React Router's useRoutes hook - DEV Community ‍ ‍
When creating multi-paged apps with React or any other library/framework, a package to handle routing is always used.
Read more >
A Complete Beginner's Guide to React Router (Including ...
Then, add it where we want to render the content. The Route component has several properties. But here, we just need path and...
Read more >
Routing in React - Pragim Tech
what is Routing in React, how to use exact in Routing in React, ... have specified the Navigation URL's and we have added...
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