有没有办法在开发模式下禁用或加速 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 验证的开发模式,原因如下:
- 它们显着降低了我的应用程序的速度。 PropType 验证是分析结果中的头号违规者,因为:
- 我有一个相当深的组件层次结构,在多个级别进行 PropType 验证(是的,我确实有适当的 shouldComponentUpdate 等)
- 我正在使用 Redux,这意味着所有更新都从层次结构的顶部或附近开始
- 我有鼠标拖动交互,争取每秒更新 30 次
- 我仍然希望看到所有其他 React 警告和错误,它们在生产模式下也会被禁用。
- 在很多情况下,Flowtype 显然可以静态验证 PropTypes。
如果没有别的,我可以为 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 显示了如何选择 ReactElementValidator
和 ReactElement
工厂来定义元素创建的工作方式:
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? 部分,看看它是否适合您。
据我所知,禁用 React PropType 验证的唯一方法是将 React 丑化为 process.env.NODE_ENV
定义为 'production'
.
但是,我想使用没有运行时 PropType 验证的开发模式,原因如下:
- 它们显着降低了我的应用程序的速度。 PropType 验证是分析结果中的头号违规者,因为:
- 我有一个相当深的组件层次结构,在多个级别进行 PropType 验证(是的,我确实有适当的 shouldComponentUpdate 等)
- 我正在使用 Redux,这意味着所有更新都从层次结构的顶部或附近开始
- 我有鼠标拖动交互,争取每秒更新 30 次
- 我仍然希望看到所有其他 React 警告和错误,它们在生产模式下也会被禁用。
- 在很多情况下,Flowtype 显然可以静态验证 PropTypes。
如果没有别的,我可以为 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 显示了如何选择 ReactElementValidator
和 ReactElement
工厂来定义元素创建的工作方式:
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? 部分,看看它是否适合您。