我有一条路线
<Route path="/server_form" exact={true} render={(props) => <ServerForm {...props} />}/>
在代码中,我有一个服务器,当我单击ServerForm的按钮时,我想通过该服务器
let server = { url: "http://localhost", description: "" }
onClick = {() => {
history.push({
pathname: "/server_form",
state: { ...server },
})
}}
我尝试从react-router用RouteComponentProps键入道具,但是没有运气:
type Server = {
url: string;
description: string;
}
interface ServerFormProps extends RouteComponentProps<Server> {
}
export function ServerForm(props: ServerFormProps) {
console.log(props.location.state!.url);
}
在console.log上,出现以下错误:
Property 'url' does not exist on type '{}'. TS2339
如何正确输入?
PS我的依赖
{
"@types/react": "^16.9.49",
"@types/react-dom": "^16.9.8",
"@types/react-router-dom": "^5.1.5",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
"typescript": "^4.0.2",
}
通过使用新的API进行了修复:
let location = useLocation<Server>()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句