Skip to content

Instantly share code, notes, and snippets.

@steve-taylor
Created April 28, 2017 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steve-taylor/351e58764782d29ea778d56e32b554de to your computer and use it in GitHub Desktop.
Save steve-taylor/351e58764782d29ea778d56e32b554de to your computer and use it in GitHub Desktop.
class MyComponent extends React.Component {
static propTypes = {
photoUrl: PropTypes.string,
initialButtonVisible: PropTypes.bool
};
static defaultProps = {
photoUrl: '',
initialButtonVisible: true
};
state = {
photoVisible: this.props.initialPhotoVisible
};
togglePhotoVisible = () => {
this.setState({
photoVisible: !this.state.photoVisible;
});
};
render() {
return (
<div>
<button onClick={this.togglePhotoVisible}>
{this.state.photoVisible ? 'Hide' : 'Show'} photo
</button>
{this.state.photoVisible && <img src={this.props.photoUrl}} />}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment