React Typescript: add location state to react router component

前端 未结 4 1742
情歌与酒
情歌与酒 2020-12-21 07:52

I have a normal route

function LoginPage(props: RouteComponentProps): React.ReactElement {...
}

that uses RouteComponentProps

4条回答
  •  失恋的感觉
    2020-12-21 08:03

    I benefitted from @Oliver Joseph Ash answer but somehow I did not have access to StaticContext perhaps because I use connected-react-router and it has no exported member but I did not dive deep.

    I created a mock type like so:

    import { RouteComponentProps } from 'react-router-dom';
    
    type LocationState = {
      // ... type of members passed to state
    };
    
    type MockType = {
      [key: string]: string | undefined;
    };
    
    type TypeWithLocationState = RouteComponentProps;
    

    This worked for me.

提交回复
热议问题