【问题标题】:RouteComponentProps location state property does not exists on type {}RouteComponentProps 位置状态属性在类型 {} 上不存在
【发布时间】:2020-05-03 11:38:27
【问题描述】:

我们开始遇到这种错误。在代码像往常一样被编译之前,我们在编译过程中没有任何错误。 但是现在一些从头开始安装 react 应用程序的人只有在我们路由到新页面并尝试从位置状态中准备好属性时才开始收到此错误。

Ts Error

这是我们正在做的读取之前工作的状态:

    interface IPageProps extends RouteComponentProps {...}
interface IState  {
  from: string;
  to: string;
}
    class Page extends Component<IPageProps, IState> {
    constructor(props: IPageProps) {
    super(props);
    const {
      location: { state: routeState },
      history,
    } = props;
    this.state = {
      from: routeState && routeState.from ? routeState.from : '',
      to: routeState && routeState.to ? routeState.to : ''
    };
  }
  }

所以错误在 routeState.from 和 routeState.to

react-routes 的版本:

  "@types/react-router": "^5.1.1",
        "@types/react-router-dom": "^4.3.5",
      "react-router": {
          "version": "5.1.2",
          "resolved": "https://registry.npmjs.org/react-router/-/react-router-5.1.2.tgz",
          "integrity": "sha512-yjEuMFy1ONK246B+rsa0cUam5OeAQ8pyclRDgpxuSCrAlJ1qN9uZ5IgyKC7gQg0w8OM50NXHEegPh/ks9YuR2A==",
          "requires": {
            "@babel/runtime": "^7.1.2",
            "history": "^4.9.0",
            "hoist-non-react-statics": "^3.1.0",
            "loose-envify": "^1.3.1",
            "mini-create-react-context": "^0.3.0",
            "path-to-regexp": "^1.7.0",
            "prop-types": "^15.6.2",
            "react-is": "^16.6.0",
            "tiny-invariant": "^1.0.2",
            "tiny-warning": "^1.0.0"
          }
        },
"react-router-dom": {
  "version": "5.1.2",
  "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-5.1.2.tgz",
  "integrity": "sha512-7BPHAaIwWpZS074UKaw1FjVdZBSVWEk8IuDXdB+OkLb8vd/WRQIpA4ag9WQk61aEfQs47wHyjWUoUGGZxpQXew==",
  "requires": {
    "@babel/runtime": "^7.1.2",
    "history": "^4.9.0",
    "loose-envify": "^1.3.1",
    "prop-types": "^15.6.2",
    "react-router": "5.1.2",
    "tiny-invariant": "^1.0.2",
    "tiny-warning": "^1.0.0"
  }
},

有人有同样的问题吗?你是怎么解决的?

令人担忧的是,这种情况以前没有发生,现在开始发生了。

提前致谢!

【问题讨论】:

标签: reactjs typescript react-router


【解决方案1】:

在您的声明中:

interface IPageProps extends RouteComponentProps {...}

你是在里面放位置吗?看来您没有从任何地方获取 location 和/或 location.state,这就是发生该错误的原因:如果您查看 this link 它可能会对 react-router 有所帮助。

【讨论】:

  • 那个接口扩展了已经定义了 location.state 的 RouteComponentProps 对吗?
猜你喜欢
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
  • 2020-10-17
  • 2019-09-23
  • 2020-08-24
  • 2015-05-12
  • 2020-04-23
相关资源
最近更新 更多