使用Kotlin时,React路由器DOM元素属性与将React组件绑定到URL存在问题。

huangapple go评论97阅读模式
英文:

Problems with Kotlin react router dom element attribuute for binding a react component to URL

问题

我目前正在开发一个示例应用程序,以学习Kotlin/Js和React库。当使用常规的JSX语法时,可以像下面这样使用BrowserRouter和Route API来设置组件以在特定URL触发时。

主要组件

导航组件如此指定,就像这个YouTube视频中所示

在尝试上述Kotlin代码时,使用KotlinDSL时类似下面的截屏

渲染组件的主要函数如下所示

然而,这会创建一个未捕获的错误异常,就像下面这样

其难以调试的原因之一是我的源映射不起作用,因此大多数调试错误实际上是在JavaScript中。不管怎样,在查找JavaScript错误后,我找到了关于不允许将组件添加为子级的新路由器更新的这篇文章

因此,我尝试通过元素属性将组件传递进去,但似乎与它们存在一些不兼容性。元素属性接受一个可选的ReactNode,我似乎无法将我的类型为props的函数组件转换为ReactNode。当我尝试通过调用create方法传递一个可选的ReactElement时,我也会出错。

我一直在苦苦挣扎Kotlin文档,似乎找不到足够的React Router实现示例。

有谁能帮我解决如何将一个函数组件传递给React Router的元素属性的问题?

英文:

I'm currently working on a sample application to learn Kotlin/Js with the react library. When using the normal JSX syntax, a component can be set to be triggered when a specific URL hits using the BrowserRouter & Route API like below.

Main Component

使用Kotlin时,React路由器DOM元素属性与将React组件绑定到URL存在问题。

Navigation Component as directed like in this youtube video
使用Kotlin时,React路由器DOM元素属性与将React组件绑定到URL存在问题。

When attempting the above code in Kotlin, it resembles something like the screenshot below using the KotlinDSL
使用Kotlin时,React路由器DOM元素属性与将React组件绑定到URL存在问题。

The main function where the component is rendered is shown below
使用Kotlin时,React路由器DOM元素属性与将React组件绑定到URL存在问题。

However, that creates an Uncaught Error Exception like the one below
使用Kotlin时,React路由器DOM元素属性与将React组件绑定到URL存在问题。

One of the reasons its also so difficult to debug is because my source maps dont work and hence most of my debugging errors are actually in Javascript. But anyways after searching up the javascript error, I found this article about the new Router update that disallows adding components as children.

As a result I tried to pass in the components through the element prop but there seems to be some incompatibility with them.
使用Kotlin时,React路由器DOM元素属性与将React组件绑定到URL存在问题。

the element prop accepts an optional ReactNode? and I cant seem to convert my Function Component of type props to a ReactNode. When I try to pass in an optional ReactElement? by calling the create method, I also get an error.

I have been struggling with the Kotlin Documentation and can't seem to find enough examples of react-router implemented.

Can anyone help me out with how to pass in a Function COmponent into the element prop for the React Router?

答案1

得分: 1

看起来你正在尝试使用遗留编译器和过时的React Router,我建议升级到更新的版本迁移到IR编译器,但我会为你留下一个示例。

看起来你应该尝试使用Outlet()组件

你可以将一个函数组件传递给元素,使用createElement(YourFunctionalComponent)函数

HashRouter {
        Routes {
            Route {
                path = "/"
                element = createElement(SomeFuctionalComponent1)
                children = Route {
                               path = "/path2"
                               element = SomeFuntionalComponent2.create()
                             }
            }
        }
    }
val SomeFuctionalComponent1 = FC<Props> {
    YourNavBarComponent()
    Outlet()
}

我还为你留下了一个示例,以防你决定切换到新版本的React Router,我从这里获取的示例

fun main() {
    val root = document.createElement(div)
        .also { document.body.appendChild(it) }

    createRoot(root)
        .render(App.create())
}

private val App = FC<Props> {
    val hashRouter = createHashRouter(
        routes = arrayOf(
            jso {
                path = "/"
                element = Showcases.create()
                errorElement = Error.create()
            },
        ),
    )


    RouterProvider {
        router = hashRouter
    }
    
}
英文:

It looks like you are trying to use the LEGACY compiler and the outdated React Router, I would recommend upgrading to the newer versions migration to IR compiler, but I'll leave an example for your case.

Looks like you should try using Outlet() component

You can pass a functional component to element as using.create()/.create { prop = propValue} and through the functioncreateElement(YourFunctionalComponent)

HashRouter {
        Routes {
            Route {
                path = "/"
                element = createElement(SomeFuctionalComponent1)
                children = Route {
                               path = "/path2"
                               element = SomeFuntionalComponent2.create()
                             }
            }
        }
    }
val SomeFuctionalComponent1 = FC<Props> {
    YourNavBarComponent()
    Outlet()
}

I also leave an example in case you decide to switch to the new version of React Router that I took from here mui showcases:

fun main() {
    val root = document.createElement(div)
        .also { document.body.appendChild(it) }

    createRoot(root)
        .render(App.create())
}

private val App = FC<Props> {
    val hashRouter = createHashRouter(
        routes = arrayOf(
            jso {
                path = "/"
                element = Showcases.create()
                errorElement = Error.create()
            },
        ),
    )


    RouterProvider {
        router = hashRouter
    }
    
}

huangapple
  • 本文由 发表于 2023年2月19日 18:08:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定