Reactjs 在渲染函数中使用箭头函数和部分应用程序之间的性能差异?
两者之间是否存在性能差异(或使一个优于另一个的其他差异) 在渲染函数中使用箭头函数:Reactjs 在渲染函数中使用箭头函数和部分应用程序之间的性能差异?,reactjs,Reactjs,两者之间是否存在性能差异(或使一个优于另一个的其他差异) 在渲染函数中使用箭头函数: <SomeElement onClick={() => this.updateSalesQuoteProgress(QuotationProgressState.Done)} /> private updateSalesQuoteProgress = (progress: QuotationProgressState): void => { this.actionSe
<SomeElement
onClick={() => this.updateSalesQuoteProgress(QuotationProgressState.Done)}
/>
private updateSalesQuoteProgress = (progress: QuotationProgressState): void => {
this.actionService.updateSalesQuoteProgress(this.props.project, progress);
};
<SomeElement
onClick={this.updateSalesQuoteProgress(QuotationProgressState.Installed)}
/>
private updateSalesQuoteProgress = (progress: QuotationProgressState): (() => void) => {
return () => {
this.actionService.updateSalesQuoteProgress(this.props.project, progress);
};
};
this.updateSalesQuoteProgress(QuotationProgressState.Done)}
/>
私有更新AllesQuoteProgress=(进度:QuotationProgressState):void=>{
this.actionService.updateSalesQuoteProgress(this.props.project,progress);
};
在渲染函数中使用部分应用程序:
<SomeElement
onClick={() => this.updateSalesQuoteProgress(QuotationProgressState.Done)}
/>
private updateSalesQuoteProgress = (progress: QuotationProgressState): void => {
this.actionService.updateSalesQuoteProgress(this.props.project, progress);
};
<SomeElement
onClick={this.updateSalesQuoteProgress(QuotationProgressState.Installed)}
/>
private updateSalesQuoteProgress = (progress: QuotationProgressState): (() => void) => {
return () => {
this.actionService.updateSalesQuoteProgress(this.props.project, progress);
};
};
私有更新SalesQuoteProgress=(进度:QuotationProgressState):(()=>void)=>{
return()=>{
this.actionService.updateSalesQuoteProgress(this.props.project,progress);
};
};
如果存在差异,请解释为什么在这两种情况下都会在每个渲染上创建返回函数,这会在您有大型应用程序和大量此类模式时影响性能 当您将组件嵌套为
PureComponent
或使用shall
比较道具实现shouldComponentUpdate
时,会出现主要问题,当您将箭头函数作为道具传递时,shall检入PureComponent
失败会导致性能问题
示例演示
类子级扩展React.Component{
应更新组件(下一步){
if(this.props.onClick==nextrops.onClick){
log(“相同的单击”);
}否则{
log(“不同的单击”);
}
if(this.props.onChange==nextrops.onChange){
log(“相同的更改”);
}否则{
console.log(“不同的更改”);
}
返回true;
}
点击=()=>{
this.props.onClick('xyx');
}
render(){
返回单击
}
}
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
计数:0
}
}
componentDidMount(){
this.timer=setInterval(()=>{
this.setState(prev=>({count:prev.count+1}));
}, 1000)
}
handleClick=(id)=>(值)=>{
console.log(id,value);
}
组件将卸载(){
clearInterval(这个计时器);
}
handleChange=(val)=>{
console.log(val)
}
render(){
返回(
this.handleChange(1)}count={this.state.count}/>
)
}
}
ReactDOM.render(,document.getElementById('app'))代码>