Skip to main content

React Native Height and Width

The height and width determine the size of component on the screen. There are two different ways to set the height and width of component: Fixed Dimensions and Flex Dimensions.

Fixed Dimensions

Using fixed height and fixed width in style is the simplest way to set the dimension of the component. The dimensions of React Native component are unit-less, and they represent density-independent pixels.
Setting the dimension of component with fixed size is common and exactly the same size, regardless of screen dimensions.

  1. import React, { Component } from 'react';  
  2. import { StyleSheet, View } from 'react-native';  
  3.   
  4. export default class HeightWidth extends Component {  
  5.     render() {  
  6.         return (  
  7.             <View>  
  8.                 <View style={styles.powderblue} />  
  9.                 <View style={styles.skyblue} />  
  10.                 <View style={styles.steelblue} />  
  11.             </View>  
  12.         );  
  13.     }  
  14. }  
  15. const styles = StyleSheet.create({  
  16.     powderblue:{  
  17.         width: 100, height: 100, backgroundColor: 'powderblue'  
  18.     },  
  19.     skyblue:{  
  20.         width: 200, height: 200, backgroundColor: 'skyblue'  
  21.     },  
  22.     steelblue:{  
  23.         height: 300, backgroundColor: 'steelblue'  
  24.     },  
  25. })  
Output
React Native Height and Width

Flex Dimensions

The flex property styles the component to expand and shrink it dynamically according to available space. Setting flex: 1 will fill all the available space to the component, and shared evenly among the other components of same as the parent. Higher the flex value, occupy component higher ratio of space compared to its siblings.
  1.  import React, { Component } from 'react';  
  2. import { StyleSheet, View } from 'react-native';  
  3.   
  4. export default class HeightWidth extends Component {  
  5.     render() {  
  6.         return (  
  7.             <View style={styles.container}>  
  8.                 <View style={styles.powderblue} />  
  9.                 <View style={styles.skyblue} />  
  10.                 <View style={styles.steelblue} />  
  11.             </View>  
  12.         );  
  13.     }  
  14. }  
  15. const styles = StyleSheet.create({  
  16.     container:{  
  17.       flex:1  
  18.     },  
  19.     powderblue:{  
  20.         flex:1,  
  21.         backgroundColor: 'powderblue',  
  22.     },  
  23.     skyblue:{  
  24.         flex:2,  
  25.         backgroundColor: 'skyblue',  
  26.     },  
  27.     steelblue:{  
  28.         flex:3,  
  29.         backgroundColor: 'steelblue',  
  30.     },  
  31. })  
Output
React Native Height and Width

Comments

Popular Posts

16 AWS Gotchas

16 AWS Gotchas In January I launched the MVP for my own startup,  Proximistyle , which helps you find what you’re looking for nearby. On advice from friends and industry contacts I chose AWS as my cloud provider. Having never had to set up my own cloud infrastructure before, the learning curve to get from no experience to a stable VPC system I was happy with was significantly steeper than expected, and had its fair share of surprises. #1 Take advantage of the free resources offered AWS offers a free tier for new accounts. If you have recently bought a domain and set up a company you qualify for the free tier for a year. Additionally, if you are a bootstrapped startup you can apply for  the Startup Builders package  and get $1000 in AWS credits. After doing the above, you’re now ready to get started with setting up the AWS infrastructure for your startup. #2 Set up billing budgets and alerting The very first thing you should do after setting up billing, is enabling a budge...

Ultimate Folder Structure For Your React Native Project

  Ultimate Folder Structure For Your React Native Project React native project structure React Native is a flexible framework, giving developers the freedom to choose their code structure. However, this can be a double-edged sword for beginners. Though it offers ease of coding, it can soon become challenging to manage as your project expands. Thus, a structured folder system can be beneficial in many ways like better organization, simplified module management, adhering to coding practices, and giving a professional touch to your project. This write-up discusses a version of a folder arrangement that I employ in my React Native projects. This structure is based on best practices and can be modified to suit the specific needs of your project. Before we get into the project structure let’s give credit to @sanjay who has the original idea of the structure but I modify his version of the code, to make it better. Base library axios  — For network calling. react-navigation ...

Master Map & Filter, Javascript’s Most Powerful Array Functions

Master Map & Filter, Javascript’s Most Powerful Array Functions Learn how Array.map and Array.filter work by writing them yourself This article is for those who have written a  for  loop before, but don’t quite understand how  Array.map  or  Array.filter  work. You should also be able to write a basic function. By the end of this, you’ll have a complete understanding of both functions, because you’ll have seen how they’re written. Array.map Array.map  is meant to transform one array into another by performing some operation on each of its values. The original array is left untouched and the function returns a new, transformed array. For example, say we have an array of numbers and we want to  multiply each number by three . We also don’t want to change the original array. To do this without  Array.map , we can use a standard for-loop. for-loop var originalArr = [1, 2, 3, 4, 5]; var newArr = []; for(var i = 0; i < originalArr.length; i+...