Hi everyone, I'm trying to use parameters from a U...
# javascript
f
Hi everyone, I'm trying to use parameters from a URL (e.g.,
/test/:id
) using the React Router wrapper and I can't make it work. I tried something similar to the example in the README (https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-react-router-dom/README.md) but that doesn't work as
match
can't be found when calling
props.match.params.id
. Does anyone know how to do this? This is my example:
Copy code
interface IdProps : RProps {
    var id: Int
}

class RootComponent : RComponent<RProps, RState>() {
    override fun RBuilder.render() {
        hashRouter { // or "browserRouter"
            switch {
                route<IdProps>("/user/:id") { child(Comp2::class) {} }
            }
        }
    }
}

class Comp2 : RComponent<IdProps, RState>() {
    
    override fun RBuilder.render() {
        div {
            +"User id: ${props.match.params.id}"
        }
    }

}
@turansky you might know about this?
🚫 1
🙂 1
After trying some things this seems to work:
Copy code
interface IdProps : RProps {
    var id: Int
}

class RootComponent : RComponent<RProps, RState>() {
    override fun RBuilder.render() {
        hashRouter { // or "browserRouter"
            switch {
                route<IdProps>("/user/:id") { props -> child(Comp2::class) { attrs.id = props.match.params.id } }
            }
        }
    }
}

class Comp2 : RComponent<IdProps, RState>() {

    override fun RBuilder.render() {
        div {
            +"User id: ${props.id}"
        }
    }

}
Although it's not ideal as I basically have to send the
id
param explicitly to the child component 🤔. It'd be great if this was already passed implicitly like it's done in JS.
👍 2