Skip to content

Instantly share code, notes, and snippets.

@tolmekian1453
Last active May 4, 2019 18:39
Show Gist options
  • Save tolmekian1453/202e53a10e82b4602b239a87cbdfcae0 to your computer and use it in GitHub Desktop.
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.
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