In ReactJS trying to get params but I get property 'id' does not exist on type '{}'
You can specify the type of the matched route parameters in the type parameter of RouteComponentProps
, so the error should disappear if you replace
class FetchData extends React.Component<RouteComponentProps<{}>, FetchDataExampleState> {
with
interface RouteParams {id: string, param2?: string}class FetchData extends React.Component<RouteComponentProps<RouteParams>, FetchDataExampleState> {
For hooks:
export interface IUserPublicProfileRouteParams { userId: string; userName: string;}const {userId, userName} = useParams<IUserPublicProfileRouteParams>();
With a React Function Component this would work:
import React from "react";import { match } from "react-router-dom";export interface AuditCompareRouteParams { fileType: string;}export const Compare = ({ match }: { match: match<AuditCompareRouteParams> }) => { console.log(match.params.fileType);};