React accessing custom state field from location.state

Viewed 592

I'm trying to implement a redirect after logging in. But there will be custom routes passed when getting to the login page which varies on which button is pressed etc.

import { useLocation } from 'react-router-dom';

const Login: FC = (): JSX.Element => {
 const location = useLocation();
 const [claimUrl, setClaimUrl] = useState({});
 useEffect(() => {
        location.state && setClaimUrl(location.state.claimUrl);
    }, [location]);
...
};

I'm getting an error: Property 'claimUrl' does not exist on type '{}'.

So is there a way I can define it on the location?

Any help/advice would be great.

1 Answers

Managed to figure this out by adding the field to useLocation().

const location = useLocation<{
        claimUrl: unknown;
    }>();
Related