Skip to main content

Flex layout component

Flex is a wrapper for CSS flex layout.

Effect demonstration

1. Horizontal direction

<Flex>
<View style={{ width: 100, height: 50, backgroundColor: 'red' }} />
<View style={{ width: 100, height: 50, backgroundColor: 'green' }} />
<View style={{ width: 100, height: 50, backgroundColor: 'gold' }} />
</Flex>
flex-ios1.png

2. Vertical direction

<Flex flexDirection="column">
<View style={{ width: 100, height: 50, backgroundColor: 'red' }} />
<View style={{ width: 100, height: 50, backgroundColor: 'green' }} />
<View style={{ width: 100, height: 50, backgroundColor: 'gold' }} />
</Flex>
flex-ios2.png

3. Center content

<Flex flex={1} justifyContent="center" alignItems="center">
<View style={{ width: 100, height: 50, backgroundColor: 'red' }} />
<View style={{ width: 100, height: 50, backgroundColor: 'green' }} />
<View style={{ width: 100, height: 50, backgroundColor: 'gold' }} />
</Flex>
flex-ios3.png

4. Automatic line wrapping

<Flex flexWrap="wrap">
<View style={{ width: '30%', height: 50, backgroundColor: 'red' }} />
<View style={{ width: '30%', height: 50, backgroundColor: 'green' }} />
<View style={{ width: '30%', height: 50, backgroundColor: 'gold' }} />
<View style={{ width: '30%', height: 50, backgroundColor: 'blue' }} />
</Flex>
flex-ios4.png

API

Basic attributes

PropertiesRequiredDescriptionTypeDefault Value
backgroundColorfalseBackground color. The value is colors in Themestring
stylefalsestyleViewStyle

For definition, see: https://reactnative.dev/docs/layout-props

PropertiesRequiredDescriptionTypeDefault Value
widthfalse
minWidthfalse
maxWidthfalse
heightfalse
maxHeightfalse
minHeightfalse
flexfalse
flexDirectionfalse
flexBasisfalse
flexGrowfalse
flexShrinkfalse
flexWrapfalse
alignContentfalse
alignItemsfalse
alignSelffalse
justifyContentfalse
aspectRatiofalse
overflowfalse

The value is spacing in Theme.

For definition, see: https://reactnative.dev/docs/layout-props

PropertiesRequiredDescriptionTypeDefault Value
marginfalse
marginStartfalse
marginEndfalse
marginHorizontalfalse
marginVerticalfalse
marginLeftfalse
marginRightfalse
marginBottomfalse
marginTopfalse
paddingfalse
paddingStartfalse
paddingEndfalse
paddingHorizontalfalse
paddingVerticalfalse
paddingLeftfalse
paddingRightfalse
paddingBottomfalse
paddingTopfalse

For the definition, see: https://reactnative.dev/docs/view-style-props

PropertiesRequiredDescriptionTypeDefault Value
borderWidthfalse
borderLeftWidthfalse
borderRightWidthfalse
borderBottomWidthfalse
borderTopWidthfalse
borderStartWidthfalse
borderEndWidthfalse
borderStylefalse
borderColorfalse
borderStartColorfalse
borderEndColorfalse
borderLeftColorfalse
borderRightColorfalse
borderBottomColorfalse
borderTopColorfalse
borderRadiusfalse
borderBottomStartRadiusfalse
borderBottomEndRadiusfalse
borderBottomLeftRadiusfalse
borderBottomRightRadiusfalse
borderTopStartRadiusfalse
borderTopEndRadiusfalse
borderTopLeftRadiusfalse
borderTopRightRadiusfalse

Flex.Item

The Flex.Item component is added with the style flex:1 by default to ensure that all items share the width equally. The children of the Flex container are not necessarily Flex.Item.