React中onMouseEnter={::this.mouseEnter}中::的含义
Meaning of :: in onMouseEnter={::this.mouseEnter} in React
我不熟悉 React 中的事件处理。在下面的代码片段中,onMouseEnter={::this.mouseEnter} 中的 :: 是什么意思?
constructor() {
this.state = {
opacity: 1
}
}
mouseEnter() {
console.log('mouse enter')
this.setState({opacity: 0.5})
}
mouseLeave() {
console.log('mouse leave')
this.setState({opacity: 1})
}
render() {
<div style={{opacity: this.state.opacity}}>
<img src={src} onMouseEnter={::this.mouseEnter} onMouseLeave={::this.mouseLeave} />
</div>
}
::
来自 ES7。相当于onMouseEnter={this.mouseEnter.bind(this)}
我不熟悉 React 中的事件处理。在下面的代码片段中,onMouseEnter={::this.mouseEnter} 中的 :: 是什么意思?
constructor() {
this.state = {
opacity: 1
}
}
mouseEnter() {
console.log('mouse enter')
this.setState({opacity: 0.5})
}
mouseLeave() {
console.log('mouse leave')
this.setState({opacity: 1})
}
render() {
<div style={{opacity: this.state.opacity}}>
<img src={src} onMouseEnter={::this.mouseEnter} onMouseLeave={::this.mouseLeave} />
</div>
}
::
来自 ES7。相当于onMouseEnter={this.mouseEnter.bind(this)}