Last active
February 2, 2018 23:32
-
-
Save JackHowa/ab6a10dcb7488ce11696a8fd071951a1 to your computer and use it in GitHub Desktop.
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 Button extends React.Component { | |
// this state can only be accessed within this one | |
// state = { timer: 1 }; | |
// handleClick = () => { | |
// this.setState((prevState) => ({ | |
// timer: prevState.timer + 1 | |
// })); | |
// }; | |
render() { | |
return ( | |
// have to execute new action | |
<button onClick={this.props.onClickFunction}> | |
+1 | |
</button> | |
); | |
} | |
} | |
// presentational component | |
// renders state merely | |
const Result = (props) => { | |
return ( | |
<div>{props.timer}</div> | |
); | |
} | |
class App extends React.Component { | |
// has the parent of both sibling components button and result | |
state = { timer: 1 }; | |
incrementCounter = () => { | |
this.setState((prevState) => ({ | |
timer: prevState.timer + 1 | |
})); | |
} | |
render() { | |
return( | |
<div> | |
<Button onClickFunction={this.incrementCounter} /> | |
<Result timer={this.state.timer} /> | |
</div> | |
) | |
} | |
} | |
ReactDOM.render(<App />, mountNode); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment