Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create circle shape in react native with flex box

this is my component:

const styles = {
 menuContainer: {
  flex: 1,
  flexDirection: 'column'  
 },
 menuItem: {
  flex: 1,
  borderRadius: ??
 }
}
        <View style={styles.menuContainer}>
            <TouchableOpacity {styles.menuItem}/>                    
            <TouchableOpacity {styles.menuItem}/> 
        </View>

bordeRadius in react native doesn't work with percentage like 50% and in flex box i don't know the width of each flexItem. do you have any idea without calculate width of each flexItem?

like image 491
hossein derakhshan Avatar asked Oct 25 '17 16:10

hossein derakhshan


People also ask

Does flexbox work in React Native?

Flexbox is designed to provide a consistent layout on different screen sizes. You will normally use a combination of flexDirection , alignItems , and justifyContent to achieve the right layout. Flexbox works the same way in React Native as it does in CSS on the web, with a few exceptions.

How do you draw a circle in react?

Just add border-radius to create a circle.

How do you make a border radius of a circle React Native?

Border Radius in React NativeThe borderRadius property can be set as the style attribute to any element. It takes a value in pixels and assigns that value to the overall border radius of that UI element. By default, the border radius of any element is 0 if you haven't added that style attribute to that element.


1 Answers

Bad news, If you don't know the container's dimensions ahead of time, then I think you're only option is to use onLayout to calculate each flex container's dimensions.

{nativeEvent: { layout: {x, y, width, height}}}

If you can declare a fixed width & height, then it's easy, but sounds like this isn't going to be news to you.

circle: {
    width: 100,
    height: 100,
    borderRadius: 100/2
}

There's a feature request submitted on this feature already. Show your support by up-voting it here...

https://react-native.canny.io/feature-requests/p/borderradius-percentages

Sorry!

like image 114
Chris Geirman Avatar answered Oct 06 '22 22:10

Chris Geirman