Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Last active February 2, 2018 23:32
Show Gist options
  • Save JackHowa/ab6a10dcb7488ce11696a8fd071951a1 to your computer and use it in GitHub Desktop.
Save JackHowa/ab6a10dcb7488ce11696a8fd071951a1 to your computer and use it in GitHub Desktop.
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