以箭头函数格式使用react的生命周期方法的优缺点 [英] Pros and cons of using react's life cycle methods in arrow function format

查看:231
本文介绍了以箭头函数格式使用react的生命周期方法的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用公共类字段语法( handler =()=> {...} )来定义我的所有React组件的事件处理程序,以便我可以为我的组件使用 this ,而不必在构造函数中绑定它们。我想知道我可以使用这种语法来使用React生命周期方法吗?假设以这种方式使用 componentWillMount componentWillMount =()=> {...}

I'm using the public class field syntax (handler = () => {...}) to define all my React components' event handlers so that I can use this for my components without binding them in the constructor. I'm wondering can I use this syntax to use React life cycle methods as well? Say use componentWillMount in this way: componentWillMount = () => {...}

如果使用箭头函数定义react的生命周期方法有什么优缺点?

What are the pros and cons if defining react's life cycle method with arrow functions?

推荐答案

每次你的函数执行 => 操作时,它必须创建一个新的函数对象。这可以防止浏览器在渲染同一元素的多个副本时重复使用相同的功能,这使得javascript引擎难以进行优化。这会导致性能问题(但在大多数程序中,它不会引人注意)。

Each time your function performs a => operation it has to create a new function object. This prevents the browser from reusing the same function when rendering multiple copies of the same element which makes optimization by the javascript engine harder to do. This would cause performance issues(, but in most programs, it won't be noticeable).

建议不要在React中使用生命周期方法的箭头函数

It is recommended not to use arrow functions for life-cycle methods in React

什么时候应该使用箭头功能

这篇关于以箭头函数格式使用react的生命周期方法的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆