Created
April 28, 2017 03:54
-
-
Save steve-taylor/5b7e1ed40926ff24459c3deefcf25587 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 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