我想爲我的RN應用程序使用propTypes
,但它似乎從未被強制執行。我的組件看起來是這樣的:反應原生propTypes不起作用
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
FIL Ë
"plugins": [
"transform-class-properties",
]
我試着製作道具需要
static propTypes = {
data: React.PropTypes.string.isRequired,
};
我甚至已經試過,沒有運氣改變export class Table...
到export default class Table...
。我已經嘗試了上面列出的方法的每個組合都無濟於事。我錯過了什麼?
我不知道你是否知道這個,但你應該使用「等號」字符傳遞一個道具
這是我的文章中的一個錯誤,我的實際代碼使用'=' ,我已經在我的帖子中修復了它 – mithunm93
React proptypes只能在開發環境中工作,所以請確認你沒有在生產env –