I am following the instructions here to use Auth0 with react-router-dom V6 and so far I am able to get it working with the basic routes.
Now I am trying to make it work with components which accept props and I not sure how to pass in the props correctly. This is the code for the ProtectedRoute
import React, { ComponentType } from 'react';
import { withAuthenticationRequired } from '@auth0/auth0-react';
import { Loading } from './Loading';
interface ProtectedRouteProps {
component: ComponentType,
props: any
}
export const ProtectedRoute: React.FC<ProtectedRouteProps> = ({
component, props
}) => {
const Component = withAuthenticationRequired(component, {
onRedirecting: () => <Loading />,
});
return <Component {...props} />;
};
And I am setting up the basic route component as
<Route path="/" element={<ProtectedRoute component={Dashboard} />}/>
My Dashboard
element expects a prop of say type string
and name testProp
. What would be the right way to pass it in? I have tried the following but none of them work correctly.
<Route path="/" element={<ProtectedRoute component={Dashboard} testProp={"test"} />}/>
<Route path="/" element={<ProtectedRoute component={Dashboard} props={{testProp: "test"}} />}/>
Can someone please provide an example of how to forward the props to the ProtectedRoute
so that it can be used as a generic wrapper? I am new to react and typescript and I am not able to figure out the right approach for this.
Edit:
With the second approach I get the following error in typescript
(property) ProtectedRouteProps.component: React.ComponentType<{}>
Type '({ testProp }: Props) => JSX.Element' is not assignable to type 'ComponentType<{}>'.
Type '({ testProp }: Props) => JSX.Element' is not assignable to type 'FunctionComponent<{}>'.
Types of parameters '__0' and 'props' are incompatible.
Property 'testProp' is missing in type '{ children?: ReactNode; }' but required in type 'Props'.ts(2322)