【问题标题】:Retrieving state from React Router v6 Navigate从 React Router v6 Navigate 检索状态
【发布时间】:2021-08-22 02:04:55
【问题描述】:

导航到新路线后,我正在尝试传递状态数据,打字稿告诉我

类型 '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>();

但是,这会引发其他错误。我该如何解决这个问题??

【问题讨论】:

    标签: reactjs typescript react-router


    【解决方案1】:

    刚刚解决了!创建接口:

    interface propState {
        email: string;
    } 
    

    然后使用

    let { email } = location.state as propState;
    

    成功了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 2022-01-17
      • 1970-01-01
      • 2022-06-28
      • 2021-12-25
      • 1970-01-01
      • 2022-08-14
      • 2023-01-17
      相关资源
      最近更新 更多