简体   繁体   中英

Preact Router: Modify URL

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.

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');
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM