technology-blog
technology-blog copied to clipboard
react项目中,constructor(){ this.target = this.func.bind(this); },JSX里onChange={this.target}的写法,为什么要比非bind的func = () => {}的写法效率高 请解释其中的原理
react项目中,constructor(){ this.target = this.func.bind(this); },JSX里onChange={this.target}的写法,为什么要比非bind的func = () => {}的写法效率高 请解释其中的原理
这题考察了函数在内存块占用执行和调度等,和对bind的深层理解。
bind之后锁定了上下文,不用向上查找
免去了向上查找执行上下文的过程
repost,get it~
多了一次函数声明 。
箭头函数每次render都是新的,this.target一直都是同一个,箭头函数会造成不必要的diff
箭头函数每次render都是新的,this.target一直都是同一个,箭头函数会造成不必要的diff
react 并不会对属性进行 diff(除了 key 属性) react 的 diff 策略是标签名字(组件名称)和 key 所以并不会造成不必要的 diff
箭头函数每次render都是新的,this.target一直都是同一个,箭头函数会造成不必要的diff
react 并不会对属性进行 diff(除了 key 属性) react 的 diff 策略是标签名字(组件名称)和 key 所以并不会造成不必要的 diff
抱歉我描述得不对 是造成不必要的render,导致 PureComponent 或 shouldComponentUpdate() 的优化失效 可以参考这里
箭头函数每次render都是新的,this.target一直都是同一个,箭头函数会造成不必要的diff
react 并不会对属性进行 diff(除了 key 属性) react 的 diff 策略是标签名字(组件名称)和 key 所以并不会造成不必要的 diff
抱歉我描述得不对 是造成不必要的render,导致 PureComponent 或 shouldComponentUpdate() 的优化失效 可以参考这里
其实你说的没问题。 render 其实就是 diff,是我忽略了 props。 props 属性值(如题所示的便是箭头回调函数)改变了的话,是会导致 PureComponent 的自动优化失效。
bind之后锁定了上下文,不用向上查找
请问箭头函数没有锁定吗
箭头函数每次render都是新的,this.target一直都是同一个,箭头函数会造成不必要的diff
题意应该是这个意思吧
class A {
foo = () => {};
}
class B {
constructor(){ this.foo = this.foo.bind(this); }
foo() {}
}
箭头函数是实例上的方法,而函数声明是在原型上的方法。
constructor() { this.target = () => { } } 这样跟上文第一种哪个效率高呢