Search code examples
javascripttypescriptpreact

How to programmatically navigate with preact-router?


I'm trying to figure out how to structure the frontend part of a web application using typescript, preact and preact-router. I've come a long way but I still need to figure out how to programmatically navigate (redirect) with preact-router. I can do history.replaceState(null, null, '/#/redirectedUrl');, but while that changes the URL in the location bar, preact-router doesn't route to the new URL.

What is the preferred way to programmatically navigate when using preact-router?


Solution

  • Importing the function route from 'preact-router' is the way to go:

    import { route } from 'preact-router';
    route('/url/to/rout/to');