Where to put helper functions of a functional component ? what's best for performance?

问题: The place we put our helpers in a functional component would affect the performance of the component ... I mean what's better when it comes to performance ... to put it out...

问题:

The place we put our helpers in a functional component would affect the performance of the component ... I mean what's better when it comes to performance ... to put it outside or inside the component?

const onPress = () => {}; // here

const MyComponent = () => {
  const onPress = () => {}; // < or here
  return (<TouchableOpacity onPress={onpress}>...</TouchableOpacity>);
};

回答1:

If you can get away with creating the function outside of the component, then that will be more efficient in that a new function will not be created every render.

Most of the time though, you need to do something specific to the component that can be impossible to do with a function outside of the component. For most cases the performance difference will be negligible.

  • 发表于 2019-03-22 00:39
  • 阅读 ( 203 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除