Skip to content

Instantly share code, notes, and snippets.

@becca-bailey
Last active August 28, 2018 19:10
Show Gist options
  • Select an option

  • Save becca-bailey/2d8a0b9502f629f53bf91a3a0495af79 to your computer and use it in GitHub Desktop.

Select an option

Save becca-bailey/2d8a0b9502f629f53bf91a3a0495af79 to your computer and use it in GitHub Desktop.
class ParentClass extends React.Component {
state = {
alert: undefined
};
render() {
return (
<SimpleComponentWithAlert renderAlert={this.renderAlert} showAlert={this.showAlert} />
)
}
private renderAlert = () => {
if (this.state.alert) {
return <FancyAlertMessage alert={this.state.alert} />
}
}
private showAlert = alert => {
this.setState({
alert
});
};
}
class SimpleComponentWithAlert extends React.Component {
render() {
const myAlert = {
message: "This is my alert",
type: "error"
};
return (
<div>
{this.props.renderAlert()}
<button onClick={this.props.showAlert(myAlert)}>Show Alert</button>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment