从 location.state 响应访问自定义状态字段
React accessing custom state field from location.state
我正在尝试在登录后实现重定向。但是在到达登录页面时会传递自定义路由,具体取决于按下哪个按钮等。
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]);
...
};
我遇到一个错误:
Property 'claimUrl' does not exist on type '{}'.
那么有什么方法可以在位置上定义它吗?
任何 help/advice 都很好。
设法通过将字段添加到 useLocation()
来解决这个问题。
const location = useLocation<{
claimUrl: unknown;
}>();
我正在尝试在登录后实现重定向。但是在到达登录页面时会传递自定义路由,具体取决于按下哪个按钮等。
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]);
...
};
我遇到一个错误:
Property 'claimUrl' does not exist on type '{}'.
那么有什么方法可以在位置上定义它吗?
任何 help/advice 都很好。
设法通过将字段添加到 useLocation()
来解决这个问题。
const location = useLocation<{
claimUrl: unknown;
}>();