reactjs - 从 React Router v6 Navigate 检索状态
问题描述
我在导航到新路线后尝试传递状态数据,打字稿告诉我
'State' 类型上不存在属性 'email'。”
父功能组件:
navigate('/check-mail', { state: { email: "hello, I'm an email" } });
子功能组件:
const SentPasswordResetInstructions = () => {
const location = useLocation();
let { email } = location.state;
}
我试过创建一个这样的界面:
interface propState { email : string }
然后使用
useLocation<propState>();
但是,这会引发额外的错误。我该如何解决 ??
解决方案
刚刚解决了!创建接口:
interface propState {
email: string;
}
然后使用
let { email } = location.state as propState;
工作!
推荐阅读
- r - R:write_xlsx 参数中有评估问题
- python - 读取 .dat 文件并包含空格
- javascript - JavaScript:根据输入字段值将查询字符串添加到 URL(如果已设置)?
- sql-server - 连接四个时计算两个表中的值
- bcp - BCP -w 复制 0 行而没有错误消息
- php - 使用 JWT 连接到 DocuSign API 的后端计划脚本
- spring-boot - Hikari 连接没有超时
- ethereum - 无法通过 Remix 部署简单的 Solidity 合约
- python - Pyspark UDF - TypeError:“模块”对象不可调用
- c# - 如何使用 AutoIT 清除远程桌面连接中的剪贴板