We have a React component XYZ that has certain UI that needs to be hidden/showed based on the route that has mounted the component.
For eg, if the route is /show -> it shall show a table if the route is /hide-> it shall hide the table.
react-router-dom is being used, but would not like to use state in history.push(...).
Am looking for a solution that can be achieved while defining the routes so that any developer who used this route later don't have to worry about maintaining the state.
PS: We are using Private Route with TS, and overriding the render method is not possible unless any is used.
interface PrivateRouteProps extends RouteProps {
}
const PrivateRoute: FunctionComponent<PrivateRouteProps> = ({
component: Component,
...rest
}) => {
if (!Component) return null;
return (
<Route
{...rest}
render={(props) => true
? <Component {...props}/> // looking for a way to pass custom props here
: <Redirect to={{ pathname: '/', state: { from: props.location } }} />}
/>
)
}
usage :
<PrivateRoute path='/show' component={XYZ} />
Any help on how can I pass props in this PrivateRoute and then pass it on to the Component will be appreciated.TIA