Flex is a core tool in React Native's Layout system. Flex is used to define how much space a view should take relative to it's siblings. There are 3 types of values for it: negative, positive and 0. When used with Views, or most of the components provided by React Native, flex's behavior closely matches that described in its docs. However when flex is used on Text it can seem slightly differenc.
To reproduce the scenarios covered in this article you can play with the code on snack.expo.io.
To reproduce the scenarios covered in this article you can play with the code on snack.expo.io.
The React Native docs do a great job describing the behavior of flex, so let's use that as our starting point:
When flex is a positive number, it makes the component flexible and it will be sized proportional to its flex value. So a component with flex set to 2 will take twice the space as a component with flex set to 1.
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 width and height. However, if there's not enough space, the component will shrink to its minWidth and minHeight.
However, if you start applying flex to side by side Text components, you may begin to notice a difference behavior as the amount of text in the view changes. If you have a lot of text in two side by side text views, each with flex 0, they will not wrap until after they are overflowing off the side of the screen. While this is a very specific annoyance, it can crop up when trying to place Text next to any element (an icon for example).
Flex of 0 with short text, looking good! |
flex of 0 with long text, the icon is cropped |
The fix to prevent this overflow is to use a flex of -1. This will allow the text to use as much width as is available to them while shrinking their width if their combined width exceeds their parent's boundaries. It is also worth noting that positive flex will still distribute the available width based on the flex applied to each component.
Flex of -1 with short text, same behavior |
Flex of -1 with long text, no more cropping |
Flex of 1 with short text for reference |
0 comments:
Post a Comment