Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preact Router: Modify URL

Tags:

routes

preact

I am using preact-router in a project. I got it working nicely, depending on the URL different components get rendered.

Now a component needs to access to add a /path to the url. Is there a way to do this? Documentation is unfortunately lacking.

I have tried to just modify the components props, hoping there would be some kind of two-way-binding, but no chance. Then I looked up the Browser History API, but I am not sure if this is the right path since it's something like a router.

like image 374
Sven Avatar asked Dec 31 '25 10:12

Sven


1 Answers

So preact-router does not give you a two way binding. It rather gives you a route method and here's how you use it

import {h, Component} from 'preact';
import {route} from 'preact-router';
export default MyComponent extends Component{
  render(props) {
    // do your logic or jsx here...
    route(props.path + '/path');
  }
}
like image 135
prateekbh Avatar answered Jan 04 '26 01:01

prateekbh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!