简体   繁体   English

反应性能和向下传递箭头功能作为道具

[英]React performance and passing down arrow functions as props

I've recently learned that passing down object literals or functions as props can cause unnecessary re-renders.我最近了解到,将对象文字或函数作为道具传递会导致不必要的重新渲染。 As I am doing an audit on my app, I am finding some cases where I have common components that have callbacks on events that do different things.当我对我的应用程序进行审核时,我发现在某些情况下,我有一些通用组件,这些组件对执行不同操作的事件进行回调。 It's unclear to me what the most elegant way to handle this would be.我不清楚处理这个问题的最优雅的方法是什么。

So for example in my app I have a component called SharedBtn that is used all over the app multiple places and in large loops.例如,在我的应用程序中,我有一个名为SharedBtn的组件,它在应用程序的多个位置和大循环中使用。 This button has an onClick listener.这个按钮有一个onClick监听器。 But in every instance this onClick is used we are passing down a different function to do a different thing every time.但是在每次使用这个onClick情况下,我们每次都会传递一个不同的函数来做不同的事情。

Example: https://codesandbox.io/s/k31120vnyo示例: https : //codesandbox.io/s/k31120vnyo

I read this related article with examples.我通过示例阅读了这篇相关文章 But their solution is to move the onClick logic to the shared component.但是他们的解决方案是将onClick逻辑移到共享组件中。 This would be ugly for me as it is used in many different spots with many different handlers.这对我来说会很丑陋,因为它在许多不同的地方使用了许多不同的处理程序。 How could I have the multiple click handlers without moving the click handling logic to the SharedBtn component itself?如何在不将点击处理逻辑移动到SharedBtn组件本身的情况下拥有多个点击处理程序?

You could create a small wrapper component for each special instance.您可以为每个特殊实例创建一个小的包装器组件。

class IndexSharedButton extends React.PureComponent {
  handleClick = e => {
    this.props.onClick(e, this.props.index);
  };
  render() {
    return <SharedBtn copy={this.props.copy} onClick={this.handleClick} />;
  }
}

class AnimalSharedButton extends React.PureComponent {
  handleClick = e => {
    this.props.onClick(this.props.animal, this.props.type);
  };
  render() {
    return (
      <SharedBtn copy={this.props.animal} onClick={this.handleClick} />
    );
  }
}

You could also manually manage caching the bound handlers, but IMO that's pretty ugly and much less idiomatic.您也可以手动管理缓存绑定处理程序,但 IMO 非常丑陋且不那么惯用。

Also, I think it goes without saying that you shouldn't worry about this much at all if you haven't measured it to be an actual issue.另外,我认为不用说,如果您还没有衡量它是一个实际问题,那么您根本不应该担心这么多。 Re-renders don't happen that often, and with shouldComponentUpdate should happen even less.重新渲染不会经常发生,并且使用 shouldComponentUpdate 应该更少发生。 Optimizations like this add more code for little benefit.像这样的优化增加了更多的代码,但收益甚微。

Most of the time the performance hit is negligible.大多数情况下,性能影响可以忽略不计。 But the correct way to mitigate this in the age of hooks is viauseCallback .但是在钩子时代缓解这种情况的正确方法是通过useCallback

import { useCallback } from "react"
const MyComp = () => {
  const func = useCallback(() => doSomething(), []);
  return <OtherComp func={func}/>
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM