Open In App

React Native View Component

In this article, We are going to see how to create a view component in react-native. For this, we are going to use React Native View component. It’s basically a small container that supports layout with flexbox, style, some touch handling, and accessibility controls. View maps directly to the native view equivalent on whatever platform React Native is running on, whether that is a UIView, <div>, android.view, etc.

This component is designed to be nested inside other views and can have 0 to many children of any type.



Syntax:

<view props="value"/>

Props for View Component:



Now let’s start with the implementation:

Step 1: Open your terminal and install expo-cli by the following command.

npm install -g expo-cli

Step 2: Now create a project by the following command.

expo init myapp

Step 3: Now go into your project folder i.e. myapp

cd myapp

Project Structure:

Example: Now let’s implement the view component. Here we created a view component inside that component we can put any API but here we will put an alert button and when someone clicks on that button an alert will pop up.

Filename: App.js




import React from 'react';
import { StyleSheet,
        Text,
        View,
        Button,
        Alert
        } from 'react-native';
  
export default function App() {
  
// Alert function
const alert = ()=>{
    Alert.alert(
    "GeeksforGeeks",
    "A Computer Science Portal",
    [
        {
        text: "Cancel",
        },
        {
        text: "Agree",
        }
    ]
    );
}
  
return (
    <View style={styles.container}>
      <Button title={"Register"} onPress={alert}/>
    </View>
);
}
  
const styles = StyleSheet.create({
container: {
    flex: 1,
    backgroundColor: 'green',
    alignItems: 'center',
    justifyContent: 'center',
},
});

Start the server by using the following command.

npm run android

Output: If your emulator did not open automatically then you need to do it manually. First, go to your android studio and run the emulator. Now start the server again. 

Reference: https://reactnative.dev/docs/view


Article Tags :