这是我当前的 React Router 实现:
const router = createBrowserRouter([
{
path: "/",
element: (
<Page activeNav="home" >
<Home />
</Page>
)
},
{
path: "/about",
element: (
<Page activeNav="about" >
<About />
</Page>
)
},
{
path: "/blog",
element: (
<Page activeNav="blog">
<Blog />
</Page>
)
},
{
path: "/blog/:postName",
element: (
<Page activeNav="blog" >
<Post />
</Page>
),
loader: ({ params }) => params.postName
},
{
path: "/chess",
element: <ChessRouter />
}
])
Run Code Online (Sandbox Code Playgroud)
最后一条路线/chess很重要。我正在寻找定义诸如/chess/play、/chess/login、 …
我将加载器与 new 一起使用,当我在组件中react-router-dom使用钩子时,响应类型未知,因此我无法使用它。useLoaderData我不想使用as类型定义,因为我觉得这是一种作弊,但如果这是唯一的方法,那么,请告诉我:
我的加载器功能:
{
path: "/reset-password/:resetToken",
element: <ResetPassword />,
loader: async ({ params }) => {
if (!params.resetToken){
return null
}
const fakeTokens = ["abcdef123456", "bingbong", "foobar"]
// make API call to check if the given token is valid
const fakeAPICall = (resetToken: string) : Promise<object> => {
if (fakeTokens.includes(resetToken)){
return new Promise(resolve => resolve({ success: true }))
}
return new Promise(resolve => resolve({ success: false }))
}
const resp = await fakeAPICall(params.resetToken); …Run Code Online (Sandbox Code Playgroud)