所以我读了
this article
Atinder Singh介绍了如何加快react原生应用程序的6种方法。
太长,读不下去了
“尽早绑定,不要在render中创建函数。
执行以下操作:
constructor(props) {
super(props);
this.doWork = this.doWork.bind(this);
}
doWork() {
// doing some work here.
// this.props.dispatch....
}
render() {
return <Text onPress={this.doWork}>Do Some Work</Text>
}
}
不是
<Text onPress={ () => this.doWork() }>Do Some Work</Text>
或
<Text onPress={ this.doWork.bind(this) }>Do Some Work</Text>
因为经常调用render,并且每次执行上述两项操作中的任何一项时,都会创建一个新函数。“-2017年10月13日
在我的环境中
this.doWork
对每个渲染调用get。所以我改成
const doWork = () => {...}
工作正常。但是我如何使用参数调用fct?当我这样做的时候
this.doWork(x, y)
,每次渲染都会再次调用fct get。
有没有一种有效的方法可以在每次渲染都不创建新的fct的情况下调用此fct?
非常感谢你的帮助!