使用 withRouter 给组件添加路由属性和函数,感觉装饰器的语法更优雅,但是 ts 下 withRouter 装饰暴露组件会出现 ts 校验错误。代码如下:
import React, { Component } from 'react';
import { withRouter, RouteComponentProps } from 'react-router-dom';
type Props = {} & RouteComponentProps;
type State = {};
@withRouter
export default class Header extends Component<Props, State> {
render() {
console.log('Header 组件的 Props:', this.props);
return <h1>This is Header</h1>;
}
}
IDE 截图:

看编辑器提示,似乎是一个 issue ,让使用 withRouter()函数调用模式,感觉有点不死心,目前没有很好的解决方案了吗?