【发布时间】:2016-12-02 00:58:59
【问题描述】:
我正在尝试将propTypes 用于我的 RN 应用程序,但它似乎从未被强制执行。我的组件看起来像这样:
import React, { Component } from "react";
import { Text } form "react-native";
export class Table extends Component {
render() {
return (<Text>...</Text>);
}
}
Table.propTypes = {
data: React.PropTypes.string,
};
这并没有警告我将数字从另一个文件传递给组件,如下所示:
<Table data= { 2000 } />
所以我尝试将propTypes 设为Table 的静态属性,因为我看到了一些关于 ES6 以这种方式使用 propTypes 的内容:
import React, { Component } from "react";
import { Text } form "react-native";
export class Table extends Component {
static propTypes = {
data: React.PropTypes.string,
};
render() {
return (<Text>...</Text>);
}
}
然后我尝试在我的.babelrc 文件中添加一个插件
"plugins": [
"transform-class-properties",
]
我已尝试将道具设为必需
static propTypes = {
data: React.PropTypes.string.isRequired,
};
我什至尝试将export class Table... 更改为export default class Table...,但没有成功。我已经尝试了上面列出的所有方法的组合,但都无济于事。我错过了什么?
【问题讨论】:
-
我不知道你是否意识到这一点,但你应该使用“等于”字符传递一个道具。 。也许这就是问题所在。
-
这是我帖子中的错字,我的实际代码使用
=,对此很抱歉,我已在帖子中修复它。 -
React proptypes 仅在开发环境中工作,因此请确认您不在生产环境中
-
嗯,我以为我已经是了,但我改变了一些东西,它开始工作了。不知道是不是环境
标签: react-native ecmascript-6 react-proptypes