我正在尝试使用最新的 React Router 6.4 为 React Router Route 定义提供动态自定义道具。我找不到任何例子来展示我如何实现这一目标。这些是由RouterProvider声明的父组件提供的 props。
6.0 - 6.3官方文档的示例:
// Ah, nice and simple API. And it's just like the <Suspense> API!
// Nothing more to learn here.
<Route path=":userId" element={<Profile />} />
// But wait, how do I pass custom props to the <Profile>
// element? Oh ya, it's just an element. Easy.
<Route path=":userId" element={<Profile animate={true} />} />
Run Code Online (Sandbox Code Playgroud)
在 6.4 中,您的路由定义类似于:
// How do I provide animate state from App …Run Code Online (Sandbox Code Playgroud) 在最近的更新(4.1 和 4.2)中,TypeScript 团队在字符串文字类型方面做了大量工作。我想知道是否有办法输入固定长度的字符串。
前任。
type LambdaServicePrefix = 'my-application-service';
type LambdaFunctionIdentifier = 'dark-matter-upgrader';
type LambdaFunctionName = `${LambdaServicePrefix}-${LambdaFunctionIdentifier}`; // error: longer than 32 characters...
Run Code Online (Sandbox Code Playgroud)
我想象它会怎样,就像,Array<64, string>;。TypeScript 具有 Tuple 类型,因此作为一个数组,我可以固定数组的长度。[string, ... string * 62, string].
type FutureLambdaIdType = `${LambdaServicePrefix}-${string[32]}`;
Run Code Online (Sandbox Code Playgroud)