Last active
June 28, 2018 16:51
-
-
Save seansullivan/c2c8d22107919989a0c1a1346af2930f to your computer and use it in GitHub Desktop.
Component Cross Talk
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
import React, { Component } from 'react'; | |
import ReactDOM from 'react-dom'; | |
class Car extends Component { | |
state = { | |
direction: null | |
}; | |
constructor(props) { | |
super(props); | |
this.handleDirectionUpdate = this.handleDirectionUpdate.bind(this); | |
} | |
handleDirectionUpdate(direction) { | |
this.setState({ direction }); | |
} | |
render() { | |
return (<div> | |
<div>🚗 Current Direction: { this.state.direction }</div> | |
<Driver updateDirection={ this.handleDirectionUpdate } /> | |
</div>); | |
} | |
} | |
class Driver extends Component { | |
constructor(props) { | |
super(props); | |
this.clickHandler = this.clickHandler.bind(this); | |
} | |
clickHandler(e) { | |
e.preventDefault(); | |
this.props.updateDirection(e.target.value); | |
} | |
render() { | |
return (<div> | |
👨 | |
<button onClick={ this.clickHandler } value="left">Turn Left</button> | |
<button onClick={ this.clickHandler } value="straight">Go Straight</button> | |
<button onClick={ this.clickHandler } value="right">Turn Right</button> | |
</div>); | |
} | |
} | |
ReactDOM.render(<Car />, document.getElementById('app')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment