Last active
May 4, 2019 18:39
-
-
Save tolmekian1453/202e53a10e82b4602b239a87cbdfcae0 to your computer and use it in GitHub Desktop.
how to make a circular image in React Native with FlexBox (not fixed width/height). Note that this may not respond properly to changes such as device rotations; that's a todo.
This file contains 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 RoundImage extends React.Component { | |
state = { borderRadius: null } | |
// tactical hacks inbound | |
componentDidMount() { | |
setTimeout(function() { // for some reason, have to do this async for it to work | |
const image = this.refs.image | |
image.measure((a, b, width, height, px, py) => { | |
this.setState({borderRadius: width / 2}) | |
}) | |
}.bind(this)) | |
} | |
render() { | |
return ( | |
<Image | |
ref="image" | |
source={this.props.source} | |
style={Object.assign(this.props.style, {aspectRatio: 1, borderRadius: this.state.borderRadius})} | |
/> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment