react-router-dom with TypeScript

前端 未结 7 575
清歌不尽
清歌不尽 2021-01-30 19:56

I\'m trying to use react router with TypeScript. However, I have certain problems using withRouter function. On the last line, I\'m getting pretty weird error:

A         


        
相关标签:
7条回答
  • 2021-01-30 20:59

    After browsing the typescript definitions I discovered the RouteComponentProps interface so I now model my containers like so

    type RouteParams = {
        teamId: string; // must be type string since route params
    }
    
    interface Props extends RouteComponentProps<RouteParams>, React.Props<RouteParams> { }
    
    type State = {
        players: Array<Player>;
    }
    
    export class PlayersContainer extends React.Component<Props, State>{} 
    

    now in the component class the route props can be accessed like this: let teamid = this.props.match.params.teamId;

    0 讨论(0)
提交回复
热议问题