Skip to content

Instantly share code, notes, and snippets.

@ILoveBacteria
Last active March 8, 2023 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ILoveBacteria/d3040527558821da2fd9bd1b4f1c6c2f to your computer and use it in GitHub Desktop.
Save ILoveBacteria/d3040527558821da2fd9bd1b4f1c6c2f to your computer and use it in GitHub Desktop.
How to handle events correctly in React
class App extends React.Component {
constructor(props) {
super(props);
this.chooseVideo = this.chooseVideo.bind(this);
}
chooseVideo(newVideo) {
this.setState({
// Change something
});
}
render() {
return (
<div>
<h1>Video Player</h1>
<Menu chooseVideo={this.chooseVideo} />
</div>
);
}
}
class Menu extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick(e) {
// The e is an event object
this.props.chooseVideo(e.target.value);
}
render() {
return (
<form onClick={this.handleClick}>
</form>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment