美文网首页我爱编程
typescript && withRouter 错误提示

typescript && withRouter 错误提示

作者: 静简明 | 来源:发表于2018-08-04 16:35 被阅读0次

1. 提示信息

when use withRouter in your component under the typescript language,it may cause error below

TS12345: Argument of type 'typeof BILink' is not assignable to parameter of type
 'ComponentType<RouterComponentProps<any, StaticContext>>'.
Type 'typeof BILink' provides no match for singnature 
'{props: RouteComponentProps<any, StaticContext} & {children?: ReactNode;}, context?: any): ReactElement<any>'

2 解决方案

interface props {
  // your props here
}
interface states {
  // attributes needed in your component here
}
export class YourComponent extends React.Component<props, states>{
  // constructor in required here
  contructor (props: any){
    super(props);
    ...
  }
}
export default withRouter(YourComponent)

another way to solve the problem

type PathParamsType {
  // type whatever you expect in the this.props.match.params.*
}
type PropsType = RouteComponentProps<PathParamsType> & {
  // your props here
}
export class YourComponent extends React.Component<PropsType>{
  ...
}
export default withRouter(YourComponent)

相关文章

网友评论

    本文标题:typescript && withRouter 错误提示

    本文链接:https://www.haomeiwen.com/subject/iesmvftx.html