Flex in React Native

1) How to use flex in react native 
Flex or flexbox has many properties like flex-direction and justify-content and align-items etc
Let's try with some random examples.

Example1
import React from 'react';
import { ViewStyleSheet } from 'react-native';
export default function Example1() {
  return (
    <View style={styles.conatainer}>
      <View style={styles.view1}/>
      <View style={styles.view2}/>
      <View style={styles.view3}/>
      <View style={styles.view4}/>
    </View>
  );
}

const styles = StyleSheet.create({
  conatainer: {
    flex: 1,
  },
  view1:{
    backgroundColor:'red',
    height:100,
    width:100
  },
  view2:{
    backgroundColor:'pink',
    height:100,
    width:100
  },
  view3:{
    backgroundColor:'yellow',
    height:100,
    width:100
  },
  view4:{
    backgroundColor:'green',
    height:100,
    width:100
  },
})


OUTPUT:


Example2
import React from 'react';
import { ViewStyleSheet } from 'react-native';
export default function Example() {
  return (
    <View style={styles.conatainer}>
      <View style={styles.view1}/>
      <View style={styles.view2}/>
      <View style={styles.view3}/>
      <View style={styles.view4}/>
    </View>
  );
}

const styles = StyleSheet.create({
  conatainer: {
    flex: 1,
    alignItems:'center'
  },
  view1:{
    backgroundColor:'red',
    height:100,
    width:100
  },
  view2:{
    backgroundColor:'pink',
    height:100,
    width:100
  },
  view3:{
    backgroundColor:'yellow',
    height:100,
    width:100
  },
  view4:{
    backgroundColor:'green',
    height:100,
    width:100
  },
})



OUTPUT:



Example 3

import React from 'react';
import { ViewStyleSheet } from 'react-native';
export default function Example() {
  return (
    <View style={styles.conatainer}>
      <View style={styles.view1}/>
      <View style={styles.view2}/>
      <View style={styles.view3}/>
      <View style={styles.view4}/>
    </View>
  );
}

const styles = StyleSheet.create({
  conatainer: {
    flex: 1,
    alignItems:'center',
    justifyContent:'center'
  },
  view1:{
    backgroundColor:'red',
    height:100,
    width:100
  },
  view2:{
    backgroundColor:'pink',
    height:100,
    width:100
  },
  view3:{
    backgroundColor:'yellow',
    height:100,
    width:100
  },
  view4:{
    backgroundColor:'green',
    height:100,
    width:100
  },
})

OUTPUT:



Example 4

import React from 'react';
import { ViewStyleSheet } from 'react-native';
export default function Example() {
  return (
    <View style={styles.conatainer}>
      <View style={styles.view1}/>
      <View style={styles.view2}/>
      <View style={styles.view3}/>
      <View style={styles.view4}/>
    </View>
  );
}

const styles = StyleSheet.create({
  conatainer: {
    flex: 1,
    flexDirection:'row',
    //alignItems:'center',
    //justifyContent:'center'
  },
  view1:{
    backgroundColor:'red',
    height:100,
    width:100
  },
  view2:{
    backgroundColor:'pink',
    height:100,
    width:100
  },
  view3:{
    backgroundColor:'yellow',
    height:100,
    width:100
  },
  view4:{
    backgroundColor:'green',
    height:100,
    width:100
  },
})


OUTPUT:



Example 5

import React from 'react';
import { ViewStyleSheet } from 'react-native';
export default function Example() {
  return (
    <View style={styles.conatainer}>
      <View style={styles.view1}/>
      <View style={styles.view2}/>
      <View style={styles.view3}/>
      <View style={styles.view4}/>
    </View>
  );
}

const styles = StyleSheet.create({
  conatainer: {
    flex: 1,
    flexDirection:'row',
    alignItems:'center',
    justifyContent:'center'
  },
  view1:{
    backgroundColor:'red',
    height:100,
    width:100
  },
  view2:{
    backgroundColor:'pink',
    height:100,
    width:100
  },
  view3:{
    backgroundColor:'yellow',
    height:100,
    width:100
  },
  view4:{
    backgroundColor:'green',
    height:100,
    width:100
  },
})

OUTPUT:



Example 6: Flex with all Views

import React from 'react';
import { ViewStyleSheet } from 'react-native';
export default function Example() {
  return (
    <View style={styles.conatainer}>
      <View style={styles.view1}/>
      <View style={styles.view2}/>
      <View style={styles.view3}/>
      <View style={styles.view4}/>
    </View>
  );
}

const styles = StyleSheet.create({
  conatainer: {
    flex: 1,
  },
  view1:{
    backgroundColor:'red',
    flex:1
  },
  view2:{
    backgroundColor:'pink',
    flex:1
  },
  view3:{
    backgroundColor:'yellow',
    flex:1
  },
  view4:{
    backgroundColor:'green',
    flex:1
  },
})

OUTPUT:



Comments

Popular posts from this blog

Button in React-Native

StyleSheet in React Native

State in React Native