高阶组件上的 PropTypes 会在生产中被移除吗?
Do PropTypes on higher order components get removed in production?
我找到了这个例子
基本上,他们在装饰器中返回组件之前将 propTypes 添加到组件中。请参阅下面的代码副本。
我知道 react strip PropTypes 的生产构建,但它会像下面这样从内部函数中剥离它吗?
function EnhanceButton(Component) {
class _EnhancedButton extends React.Component {
render() {
return (
<Component { ...this.props }>{this.props.children}</Component>
);
}
}
_EnhancedButton.propTypes = Component.propTypes;
return _EnhancedButton;
}
您可以在代码 here 中看到,仅当 __DEV__
为真时才会检查道具类型。 propTypes
不会从生产构建中的代码中删除,只是跳过检查以获得更好的性能。
在你的情况下它也会被跳过。
React 不会删除代码的 propTypes
,它只是跳过生产中的 propTypes
验证。
如果你想删除生产中的propTypes
相关代码,你可以使用babel-plugin-transform-react-remove-prop-types
。
此外,您可以使用babel-react-optimize
做进一步的优化。
我找到了这个例子
基本上,他们在装饰器中返回组件之前将 propTypes 添加到组件中。请参阅下面的代码副本。
我知道 react strip PropTypes 的生产构建,但它会像下面这样从内部函数中剥离它吗?
function EnhanceButton(Component) {
class _EnhancedButton extends React.Component {
render() {
return (
<Component { ...this.props }>{this.props.children}</Component>
);
}
}
_EnhancedButton.propTypes = Component.propTypes;
return _EnhancedButton;
}
您可以在代码 here 中看到,仅当 __DEV__
为真时才会检查道具类型。 propTypes
不会从生产构建中的代码中删除,只是跳过检查以获得更好的性能。
在你的情况下它也会被跳过。
React 不会删除代码的 propTypes
,它只是跳过生产中的 propTypes
验证。
如果你想删除生产中的propTypes
相关代码,你可以使用babel-plugin-transform-react-remove-prop-types
。
此外,您可以使用babel-react-optimize
做进一步的优化。