Created
October 11, 2016 13:45
-
-
Save cef62/d9afcae88210fc2c81b76d7982ca9c29 to your computer and use it in GitHub Desktop.
Which is the preferred way to manage on-demand event like onMouseMove?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Comp extends React.Component { | |
constructor(props) { | |
super(props) | |
this.onMouseDown = this.onMouseDown.bind(this) | |
this.onMouseMove = this.onMouseMove.bind(this) | |
this.onMouseUp = this.onMouseUp.bind(this) | |
this.state = { onMouseMove: null, onMouseUp: null } | |
} | |
onMouseDown(evt) { | |
this.setState({ onMouseMove: this.onMouseMove, onMouseUp: this.onMouseUp }) | |
} | |
onMouseMove(evt) { | |
// do something | |
} | |
onMouseUp() { | |
this.setState({ onMouseMove: null, onMouseUp: null }) | |
} | |
render() { | |
const { onMouseMove, onMouseUp } = this.state | |
return ( | |
<div> | |
<button | |
onMouseDown={this.onMouseDown} | |
onMouseMove={onMouseMove} | |
onMouseUp={onMouseUp} | |
>drag</button> | |
</div> | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Comp extends React.Component { | |
constructor(props) { | |
super(props) | |
this.onMouseDown = this.onMouseDown.bind(this) | |
this.onMouseMove = this.onMouseMove.bind(this) | |
this.onMouseUp = this.onMouseUp.bind(this) | |
this.dragEnabled = false | |
} | |
onMouseDown(evt) { | |
this.dragEnabled = true | |
} | |
onMouseMove(evt) { | |
if (this.dragEnabled) { | |
// do something | |
} | |
} | |
onMouseUp() { | |
this.dragEnabled = false | |
} | |
render() { | |
return ( | |
<div> | |
<button | |
onMouseDown={this.onMouseDown} | |
onMouseMove={this.onMouseMove} | |
onMouseUp={this.onMouseUp} | |
>drag</button> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment