答案:试过各种方案,都很复杂,不行,只能用hook。
除了把Component改造成FC,还有个方案是自己包一层FC:
MyWrapper:
import { useParams } from 'react-router-dom'; import YourComponent from './YourComponent '; function MyWrapper() { const { id } = useParams(); return ( <YourComponent id={Number(id)} /> ); } export default MyWrapper
YourComponent:
import { Component } from 'react'; import './YourComponent.css' interface Prop { id: number } interface State { } let DEFAULT_ROWS = 20 let DEFAULT_COLS = 6 class YourComponentextends Component<Prop, State> { ... }