【发布时间】:2019-01-07 07:58:18
【问题描述】:
我遇到了一个问题,其中我有一个无状态组件,它从 react-router-dom 传递 History 对象,并通过 props 将该对象传递给有状态对象。 Typescript 似乎不认为我可以将历史对象作为道具传递下去。
这是我的组件
import { History } from 'history';
import * as React from 'react';
import { RouteComponentProps, withRouter } from 'react-router-dom';
const SignInPage = ({ history }: { history: History }) =>
<div>
<h1>SignIn</h1>
<SignInForm history={history} />
</div>
class SignInForm extends React.Component<RouteComponentProps<{}>, {}> {
constructor(props: RouteComponentProps<{}>) {
super(props);
}
public handleClick = () => {
const { history } = this.props;
history.push('/home')
};
public render() {
return (
<button onClick={this.handleClick}>Sign In!</button>
);
}
}
export default withRouter(SignInPage);
export { SignInForm };
我得到的错误是这个。
Type '{ history: History; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<SignInForm> & Readonly<{ children?: ReactNode; }> ...'.
Type '{ history: History; }' is not assignable to type 'Readonly<RouteComponentProps<{}, StaticContext>>'.
Property 'location' is missing in type '{ history: History; }'.
【问题讨论】:
标签: reactjs typescript react-router-dom