Skip to content

Instantly share code, notes, and snippets.

@steve-taylor
Created April 28, 2017 03:54
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/5b7e1ed40926ff24459c3deefcf25587 to your computer and use it in GitHub Desktop.
Save steve-taylor/5b7e1ed40926ff24459c3deefcf25587 to your computer and use it in GitHub Desktop.
class MyComponent extends React.Component {
constructor(props) {
this.state = {
photoVisible: this.props.initialPhotoVisible
};
this.togglePhotoVisible = this.togglePhotoVisible.bind(this);
}
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>
);
}
}
MyComponent.propTypes = {
photoUrl: PropTypes.string,
initialButtonVisible: PropTypes.bool
};
MyComponent.defaultProps = {
photoUrl: '',
initialButtonVisible: true
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment