【问题标题】:React Native propTypes not workingReact Native propTypes 不起作用
【发布时间】: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


【解决方案1】:

当我摆弄代码时,问题似乎自行消失了。这可能是@asaf david 建议的环境问题,我不太确定。我试着回去把东西改回来看看我是否可以重现,但我不能:(。我很抱歉将来搜索这个的人。

【讨论】:

  • 我遇到了同样的问题。你怎么能解决这个问题?在 VSCode 中按 CTRL + Space 时,我没有看到警告或推荐
  • 就像我说的,它以某种方式修复了自己,并尽我所能尝试我无法重现该问题,对不起!
猜你喜欢
  • 1970-01-01
  • 2017-11-22
  • 2018-03-29
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2021-03-08
  • 2018-06-20
  • 2017-12-29
相关资源
最近更新 更多