Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Stylesheet: what does {flex:1} do?

React Native uses flexbox for layout. In all of the examples I've seen, they do something like this:

var styles = StyleSheet.create({   container: {     flex: 1,     flexDirection: 'row'   } }); 

I'm curious about the flex: 1 part. Based on Chris Coyier's definition here https://css-tricks.com/snippets/css/a-guide-to-flexbox/, flex: 1 should be the same as flex-grow: 1, but to me it looks like flex: 1 in React Native is equivalent to display: flex in CSS.

Here's a CodePen that demonstrates that flex: 1 the way React Native examples use it doesn't do anything in CSS:

http://codepen.io/johnnyo/pen/BoKbpb

It's not until we use display: flex in CSS until flexbox starts to work:

http://codepen.io/johnnyo/pen/epZXgz

So does this mean that flex: 1 in React Native is equivalent to display: flex in CSS?

like image 220
Johnny Oshika Avatar asked Sep 18 '15 20:09

Johnny Oshika


People also ask

What does flex 1 mean in React Native?

Normally you will use flex: 1 , which tells a component to fill all available space, shared evenly amongst other components with the same parent. The larger the flex given, the higher the ratio of space a component will take compared to its siblings.

What Flex 1 does?

flex: 1 sets flex-grow to 1 (whereas the default value is 0 ). What this does: If all items have flex-grow set to 1, the remaining space in the container will be distributed equally to all children.

What is the use of flex 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.

What does flex 0 mean in React Native?

When flex is 0, the component is sized according to width and height , and it is inflexible. When flex is -1, the component is normally sized according to width and height . However, if there's not enough space, the component will shrink to its minWidth and minHeight .


2 Answers

There is quite a difference between css flexbox and the one implemented by Facebook. Lots of things in common but defaults are very different. Specifically:

Everything is display: flex by default. All the behaviors of block and inline-block can be expressed in term of flex but not the opposite. 

flex: attribute is only used when at the same level there are few components with different flex values (flex: 1, flex: 3) means that the second element should be 3 times bigger than the first one. flex attribute is the only one supported (no grow/shrink support).

More info: https://github.com/facebook/css-layout

like image 119
Jarek Potiuk Avatar answered Oct 06 '22 12:10

Jarek Potiuk


A remark to Jarek Potiuk's answer: 'flex: 1' does do something in react-native similar to flex-grow behavior. Even if it is the only one with flex: defined.

Styles such as flexDirection, alignItems, justifyContent all define styling of children of the element. Similar to CSS Display: flex, which also defines children.

In contrast, flex: x defines the element itself.

E.g. if a container component has flexDirection: 'row', alignItems: 'center'
And there are 3 children:

child 1 has width 50

child 2 has flex 1 (or any other number, but 1 is common practice)

child 3 has width 50

Then the middle component will 'stretch' so that the 3 children together fill the entire width of the parent component.

like image 26
wintvelt Avatar answered Oct 06 '22 13:10

wintvelt