mobx-state-router icon indicating copy to clipboard operation
mobx-state-router copied to clipboard

Set URI fragment/hash

Open hkeide opened this issue 4 years ago • 7 comments

Hello, thanks for a great library, finally something sane and simple.

Can I set the hash part of a URL in a link? I've looked everywhere and I can't find it mentioned anywhere.

Also, is there a good way to navigate to a completely custom full URL without having the library build the href? For example I get a sent a full URL by the server and I want to navigate to it without knowing which route it goes to. Should I just use the browser history API directly for this use case?

hkeide avatar Mar 12 '21 11:03 hkeide

Hi @hkeide, this router was designed with the assumption of push state. May I know why you need to go for hash part?

For your second question, the router only knows about the specified routes sent to it in the constructor. I think it should be ok to use the history API outside of the router. Let me know how this works out for you.

nareshbhatia avatar Mar 14 '21 06:03 nareshbhatia

Hi, I have a long feed page, and I want to be able to scroll to a specific location on the page using a link without having to reload the SPA

hkeide avatar Mar 14 '21 06:03 hkeide

If I am not mistaken, you should be able to render links on that page with associated hashes and they should jump to the right location. For example: <a href="/departments/electronics#cannon">Go to Cannon</a>. If it causes a page refresh, you can encapsulate this in a component and prevent the refresh, something like this.

nareshbhatia avatar Mar 14 '21 14:03 nareshbhatia

Doesn't a raw a tag like that always cause a page reload? It seems I have to override routerStateToUrl() so that routerStore.goToState() will do the right thing in the onClick handler? I can't see a way to do this without forking the library. Please correct me if I'm wrong. I can create a PR for adding fragment capability if you're interested.

hkeide avatar Mar 17 '21 14:03 hkeide

Doesn't a raw a tag like that always cause a page reload?

@hkeide, I believe that is correct.

If it causes a page refresh, you can encapsulate this in a component and prevent the refresh, something like this.

@nareshbhatia, it's somewhat discouraging that I need to basically rewrite that entire component in order to create hash-links. Is there no better solution?

laustdeleuran avatar Nov 09 '23 00:11 laustdeleuran

Hi @laustdeleuran, please feel free to submit a PR for this.

nareshbhatia avatar Nov 10 '23 16:11 nareshbhatia

@nareshbhatia I actually looked into forking the RouterLink component to support hashes, but I don't see how I could even do that. The RouterLink component uses RouterState.goToState under the hood, which also doesn't support hashes, so forking the code in that component seems like it wouldn't work at all.

Effectively, it seems like I'd have to extend RouterState.goToState, but that is just a shallow wrapper on top of the TransitionState class. And I don't see any event hooks or public API that would allow me to extend that.

So, in other words, I'd have to submit a PR modifying the core functionality of this library, which I'm honestly down for, but I would love some more input on where this functionality should live. It feels clear to me that it should not live in the RouterLink component, but it feels pretty opaque where would be a more appropriate place for it.

laustdeleuran avatar Nov 10 '23 17:11 laustdeleuran