反应从 location.state 访问自定义状态字段 [英] React accessing custom state field from location.state

查看:64
本文介绍了反应从 location.state 访问自定义状态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在登录后实现重定向.但是在进入登录页面时会传递自定义路由,这会因按下哪个按钮等而异.

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]);
...
};

我收到一个错误:属性claimUrl"在类型{}"上不存在.

那么有没有办法在位置上定义它?

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

任何帮助/建议都会很棒.

Any help/advice would be great.

推荐答案

通过将字段添加到 useLocation() 设法解决了这个问题.

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

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

这篇关于反应从 location.state 访问自定义状态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆