Created
August 8, 2018 09:49
-
-
Save VivienAdnot/153d2e61e0fc20364e4753dd3d80bf7a to your computer and use it in GitHub Desktop.
event bubbling poc: checkbox input -> label -> div
This file contains 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
import React, { Component } from "react"; | |
import ReactDOM from "react-dom"; | |
import "./styles.css"; | |
class App extends Component { | |
state = { | |
selected: false | |
}; | |
toggle() { | |
console.log("toggle called"); | |
this.setState(prevState => { | |
prevState.selected = !prevState.selected; | |
return prevState; | |
}); | |
} | |
render() { | |
return ( | |
<div | |
className="App" | |
style={{ padding: "10px", border: "1px solid blue" }} | |
onClick={ev => { | |
console.log("onclick triggered on", ev.target); | |
ev.stopPropagation(); | |
this.toggle(); | |
}} | |
> | |
<label | |
onClick={ev => { | |
console.log("onclick triggered on", ev.target); | |
ev.stopPropagation(); | |
}} | |
> | |
<input | |
onClick={ev => { | |
console.log("onclick triggered on", ev.target); | |
ev.stopPropagation(); | |
this.toggle(); | |
}} | |
checked={this.state.selected} | |
type="checkbox" | |
/> | |
click me | |
</label> | |
</div> | |
); | |
} | |
} | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment