更新:我找到了一种新方法来做这种事情。与 b4 相同,您需要安装它们类型:
1.- npm i react-router react-router-dom
2.- npm i -D @types/react-router @types/react-router-dom
import React from "react";
import { RouteComponentProps } from "react-router-dom";
interface MyComponentProps extends RouteComponentProps {
someOfYourOwnProps: any;
someMorePropsIfNeedIt: any;
}
interface MyComponentState {
someProperty: any;
another: any;
}
export class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
public state: MyComponentState;
public constructor (props: MyComponentProps) {
super(props);
this.state = {
someProperty: "",
another: ""
}
}
public onClickHandler (evt: React.MouseEvent<HTMLButtonElement, MouseEvent>): void {
evt.preventDefault();
}
public componentDidMount () {
this.props.history;
}
public render (): React.ReactElement<MyComponentProps> {
return (
<div>trol</div>
)
}
}
hihitl 我知道发生了什么。希望你仍然需要它。
1.- npm i react-router react-router-dom
2.- npm i -D @types/react-router @types/react-router-dom
import React from "react";
import { History, LocationState } from "history";
interface MyComponentProps {
someOfYourOwnProps: any;
history: History<LocationState>;
someMorePropsIfNeedIt: any;
}
然后在你的组件上,如果它是一个类做
class MyComponent extends Component<MyComponentProps, {}> {}
如果是函数式
const MyComponent = (props: MyComponentProps) => {}