我有以下基本组件
import * as React from 'react';
import { withRouter, RouteComponentProps } from 'react-router-dom';
export interface Props { };
@withRouter
export default class Movies extends React.PureComponent<Props> {
goBack = () => {
this.props.history.goBack();
};
public render() {
return (
<div>
<button onClick={this.goBack}>Back</button>
</div>
);
}
}
我希望 withRouter
将特定的 props 注入(inject)我的组件。但是,当引用 this.props.history
时,我得到了这个:
使用将添加类型定义的装饰器的合适方法是什么?
最佳答案
我是这样做的:
import * as React from 'react';
import { withRouter, RouteComponentProps } from 'react-router-dom';
export interface OwnProps { };
type Props = OwnProps & RouteComponentProps<{}>;
@withRouter
export default class Movies extends React.PureComponent<Props> {
goBack = () => {
this.props.history.goBack();
};
public render() {
return (
<div>
<button onClick={this.goBack}>Back</button>
</div>
);
}
}
关于javascript - 如何在 TypeScript 中使用实验性装饰器和协调类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47888934/