相关疑难解决方法(0)

反应路由器中 location.state 的类型或接口

在一个组件中,有一个<Link>(来自 react-router-dom)在 state 属性中传递一个对象。

调用的另一个组件ReceiverComponent是正确接收该对象。但是,下面的代码抱怨消息:

类型“PoorMansUnknown”不可分配给类型“locationStateProps”。类型 'undefined' 不可分配给类型 'locationStateProps'.ts(2322)

type locationStateProps = {
  name: string;
}

function ReceiverComponent() {
  const location = useLocation();
  const myState: locationStateProps = location.state;
  return (
    <div>
      {myState.name}
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

我需要以某种方式为状态提供一种类型,而不会出现此错误。如何进行?

typescript reactjs react-router react-router-dom

6
推荐指数
2
解决办法
3147
查看次数