[英]TS2339: Property 'PropTypes' does not exist on type 'typeof React' when porting to Typescript
I'm currently converting a javascript/react project to a typescript/react/redux project. 我目前正在将一个javascript / react项目转换为typescript / react / redux项目。
I'm currently having a problem with this file: 我目前遇到此文件的问题:
import React from 'react';
import GoldenLayout from 'golden-layout';
import {Provider} from 'react-redux';
import Default from '../modules/m-Default/Default';
interface GoldenLayoutWrapperProps {}
interface GoldenLayoutWrapperState {}
class GoldenLayoutWrapper extends React.Component <GoldenLayoutWrapperProps, GoldenLayoutWrapperState> {
public layout;
static contextTypes = {
store: React.PropTypes.object.isRequired
};
private wrapComponent(Component, store) {
class Wrapped extends React.Component {
render () {
return (
<Provider store={store}>
<Component {...this.props}/>
</Provider>
)
}
}
return Wrapped;
}
componentDidMount() {
var layout = new GoldenLayout(this.layoutConfig, this.layout);
layout.registerComponent('Default', this.wrapComponent(Default, this.context.store));
}
render() {
return (
<div className="golden-layout-wrapper" ref={input => this.layout = input}/>
)
}
private layoutConfig = {}
}
export default GoldenLayoutWrapper;
When I attempt to compile, I get the following error: 当我尝试编译时,我收到以下错误:
ERROR in [at-loader] ./src/main/GoldenLayoutWrapper.tsx:18:22
TS2339: Property 'PropTypes' does not exist on type 'typeof React'.
After some searcing, some people suggested to give contextTypes
a static modifier, which I attempted, but the problem still persists. 在一些搜索之后,有些人建议给
contextTypes
一个静态修饰符,我尝试过,但问题仍然存在。
What could be the problem? 可能是什么问题呢?
As you are using React v16.0.0 , You need to make use of prop-types
as it is treated a separate library since React v15.5 当您使用React v16.0.0时,您需要使用
prop-types
因为它自React v15.5起被视为一个单独的库
You can install it via npm as a project dependency 您可以通过npm作为项目依赖项安装它
npm install --save prop-types
And then inside your component where you want to use it : 然后在您想要使用它的组件内:
import PropTypes from 'prop-types'; // ES6
Instead of React.PropTypes
you can directly use PropTypes 取而代之的
React.PropTypes
你可以直接使用PropTypes
Check my answer here for full details: Cannot read property 'string' of undefined | 在这里查看我的答案以获取完整的详细信息: 无法读取未定义的属性'string' React.PropTypes |
React.PropTypes | LayoutPropTypes.js
LayoutPropTypes.js
In short, as of React v15.5 React.PropTypes
is deprecated. 简而言之,从React v15.5
React.PropTypes
,不推荐使用React.PropTypes
。 You'll need to install the prop-types
package from npm. 您需要从npm安装
prop-types
包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.