Downside of binding in the constructor: react hot loader won't work.
Downside of binding in render(): performance.
Recently I've been doing this. It's slightly faster than binding in render, but I'm willing to trade the performance for flexibility and my coveted HMR.
render(){
return <input onChange={(e) => this.handleChange(e.target.value)}>;
}
It gives a little more flexibility, for example, and easier transition to the canonical Input atom.
render(){
return <input onChange={(x) => this.handleChange(x)}>;
}
Or adding arguments where you want them:
render(){
return (
<ul>
{this.props.data.map((x, i) => {
// contrived example
return (
<li
onMouseMove={(e) => this.handleMove(i, e.pageX, e.pageY)}>
{x}
</li>
);
}}
</ul>
);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…