有没有办法在开发模式下禁用或加速 React PropType 验证?

is there any way to disable or speed up React PropType validation in development mode?

据我所知,禁用 React PropType 验证的唯一方法是将 React 丑化为 process.env.NODE_ENV 定义为 'production'.

但是,我想使用没有运行时 PropType 验证的开发模式,原因如下:

如果没有别的,我可以为 babel-plugin-react-transform 创建一个转换器,它会去除所有组件的 propTypes (或者可能只是那些我以某种方式注释的组件),但是我'我想知道是否有更简单的方法来做到这一点,因为 React 可以 轻松提供编译时标志来禁用 PropType 验证。

更新: 那个 babel 插件已经存在 (https://www.npmjs.com/package/babel-plugin-react-remove-prop-types)

简答:没有简单的标志来仅禁用 PropType 验证


目前,PropType 验证由 __DEV__ 全局变量启用。 如果它更改为 false,您将丢失其他 React 警告和错误,如您所说,您不能。

此代码 here in ReactDOMFactories 显示了如何选择 ReactElementValidatorReactElement 工厂来定义元素创建的工作方式:

function createDOMFactory(tag) {
  if (__DEV__) {
    return ReactElementValidator.createFactory(tag);
  }
  return ReactElement.createFactory(tag);
}

ReactElementValidator.createElement you can see that it calls ReactElement.createElement and then validatePropTypes中:

var ReactElementValidator = {

  createElement: function(type, props, children) {

    /* some code here */

    var element = ReactElement.createElement.apply(this, arguments);

    /* some code here */

    // here would be a good place for the flag that you need
    validatePropTypes(element);

    return element;
  }

我不确定这些信息对您有何帮助,但至少表明没有您想知道的通过标志禁用 PropType 的简单方法。


更新 - 10/May/2017
Andy found that there is a Babel Plugin that removes Prop Types.
我没有测试过。请务必阅读插件的 Is it safe? 部分,看看它是否适合您。