使用 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()函数调用模式,感觉有点不死心,目前没有很好的解决方案了吗?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.