Is this (<https://github.com/hfhbd/routing-compose...
# compose-web
s
Is this (https://github.com/hfhbd/routing-compose) currently the only way to get HashRouter/BrowserRouter-like functionality with Compose Web? I can't figure out how to do a relative link with this
b
It is. Raise an issue if you're missing something
h
s
Also it's only onClick? Is there an equivalent to React's <Link>?
h
What do you mean with onClick? You can use
val router = Router.current
in your composable and use
router.navigate(to="/foo")
everywhere.
s
@hfhbd still can't quite figure out how to use it build.gradle.kts: https://paste2.org/VZc2zCaP Can't import HashRouter or anything like it from the main() function
Oh it says Could not find app.softworkrouting compose js0.0.20.
h
Why do you use such an old version? 0.0.20 and before are only published on GitHub packages, 0.0.21+ are published on mavenCentral
s
oh
I dunno where else to find the version history
h
Latest version is 0.1.8
s
ah thanks
the nav links don't look like links though, they're just clickable text
h
What do you expect? According to React Link, it matches the behavior.
s
I dunno, IIRC React links look like <a href> links and shows the destination URL on hover
b
Looks like one of you is talking about styling while the other about layout and functionality
h
Yeah, after testing, react sets the
href
tag, I don’t.
s
well it's not "just" styling. From what I can tell there's no way to convert it to what I want/need using just CSS Could change the color and stuff like that but it still won't work exactly like HTML hyperlinks. No destination hint, and won't change color to indicate visited
h
You could set the href tag by yourself, but you need to call
event.preventDefault()
s
yeah, I dunno I need something that will calculate relative paths and hyperlink naturally to them
b
You can always create your own composable that wraps and configures NavLink and reuse that
👍 1
h
Copy code
@Composable
public fun NavLink(
    to: String,
    attrs: (AttrsScope<HTMLAnchorElement>.() -> Unit)? = null,
    content: @Composable () -> Unit
) {
    val router = Router.current
    A(
        href = to,
        attrs = {
            attrs?.invoke(this)
            onClick {
                router.navigate(to)
                it.preventDefault()
            }
        }
    ) { content() }
}
s
Hm I've to think about it I guess the "hard part" of routing is translating the URI to the resource and that's already done I guess
a
Decompose supports BrowserRouter as well https://arkivanov.github.io/Decompose/router/browser-history/
👀 1
b
Just looked through decompose docs and I couldn't find any mention of permalink support, hash routing or query parameters. Does it mean all of that is missing for now?
a
I'm not sure if any special support is actually needed. At the moment, you manually parse the URL and determine what subtree it corresponds. And vice versa. Hashes should also work I guess, but I didn't actually try.
o
Does anyone know how to pass parameters? I did like this
Router.current.nanavigate("/task?page=1")
But I can't find a way to get the page number. This returns null
parameters.map
h
https://routing.softwork.app/#/users/0?name=paul That's strange, it should work. Could you create an issue? I will take a look later.
o
Sure
@hfhbd sorry, it's my bad; it works fine. I formatted the URL incorrectly. I put this
navigate("/task/page=1")
instead of
navigate("/task?page=1")
All good 👍