【发布时间】:2018-05-29 15:09:17
【问题描述】:
这篇文章(和其他地方)现在提到在渲染内部调用函数:
https://levelup.gitconnected.com/optimize-react-performance-c1a491ed9c36?ref=reddit
我一直对大型 React 类组件使用一种模式,我会将一些 JSX 从渲染函数移到方法中。这避免了有十亿个一次性使用的单独组件,还避免了在渲染区域内放置冗长的 if/then 或三元逻辑,我发现 稍微 难以阅读。
class HelpModal extends React.Component {
state = { visible: false };
renderContent = () => {
if (this) {
return <div />
}
if (that) {
return <span />
}
}
render() {
return (
<Modal visible={this.state.visible}>
{this.renderContent()}
</Modal>
);
}
}
我在很多地方都看到过这种策略,但现在听起来,根据上面的博客文章,这在性能方面可能是一种不好的做法?
【问题讨论】:
标签: reactjs