首页 > 解决方案 > 从 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>();

但是,这会引发额外的错误。我该如何解决 ??

标签: reactjstypescriptreact-router

解决方案


刚刚解决了!创建接口:

interface propState {
    email: string;
} 

然后使用

let { email } = location.state as propState;

工作!


推荐阅读