How to create a Surface in react-native ?
Last Updated :
07 Mar, 2024
React native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render mobile UI's for both platforms.
Prerequisites:
Approach: In this article, we will see that how to create a surface in react-native. Surface is like a container that uses depth and elevation. Whenever it is rendered, it gives Card Like view. we can add elevation property to give it required depth. It can be used in different scenarios, for example: To show notices, upcoming events, to convey urgent messages etc. We can customise its size according to the need.
In our project, we will initially show a text which has the pressable capability. On press of that text, all the upcoming events will appear below in a surface view. We will see the approach step-by-step.
Below is the step by step implementation:
Step 1: Create a project in react-native using the following command:
npx react-native init DemoProject
Step 2: Install react-native paper using the following command:
npm install react-native-paper
Step 3: Create a components folder inside your project. Inside the components folder create a file SurfaceComponent.js
Project Structure: It will look like this.
Implementation: Write down the code in respective files.
We will import Surface Component directly from react-native-paper library. To apply Dark theme to Surface, we can use DarkTheme from react-native-paper.
SurfaceComponent.js
import React, { useState, useEffect } from 'react';
import { Text, View, StyleSheet, Pressable } from 'react-native';
import { DarkTheme, Surface } from 'react-native-paper';
const SurfaceExample = () => {
const [HideSurface, setHideSurface] = useState(true);
const [events] = useState([
'Singing show from 7pm to 8pm ',
'Drama show from 8pm to 9pm',
'Online Get-Together from 10pm to 11pm',;
]);
let c = 0;
return (
<View>
<Pressable onPress={() => setHideSurface(false)}>
<Text style={styles.text}> Upcoming events</Text>
</Pressable>
{HideSurface ? (
<></>
) : (
events.map((event) => {
return (
<Surface style={styles.surface} theme={DarkTheme} key={c++}>
<Text style={styles.surfaceText}>{event}</Text>
</Surface>
);;
})
)}
</View>
);;
};
export default SurfaceExample;
const styles = StyleSheet.create({
text: {
fontSize: 30,
fontWeight: 'bold',
margin: 20,
},
surface: {
margin: 25,
padding: 10,
height: 80,
width: 150,
elevation: 6,
borderRadius: 4,
},
surfaceText: {
color: 'white',
},
});
App.js
import React from "react";
import type { Node } from "react";
import { View } from "react-native";
import SurfaceExample from "./components/SurfaceComponent";
const App: () => Node = () => {
return (
<View>
<SurfaceExample />
</View>
);
};
export default App;
Step to run the application: Run the application using the following command:
npx react-native run-android
Output:
Similar Reads
MVC Framework Introduction Over the last few years, websites have shifted from simple HTML pages with a bit of CSS to incredibly complex applications with thousands of developers working on them at the same time. To work with these complex web applications developers use different design patterns to lay out their projects, to
6 min read
Best Way to Master Spring Boot â A Complete Roadmap In the corporate world, they say "Java is immortal!". But Why? Java remains one of the major platforms for developing enterprise applications. Enterprise Applications are used by large companies to make money. Those applications have high-reliability requirements and an enormous codebase. According
14 min read
Spring Boot - Architecture Spring Boot is built on top of the core Spring framework. It simplifies and automates Spring-based application development by reducing the need for manual configuration. Spring Boot follows a layered architecture, where each layer interacts with other layers in a hierarchical order. The official Spr
3 min read
Bash Scripting - If Statement Bash is an interpreter for command languages. It is a default command interpreter on most GNU/Linux systems and is widely available on various operating systems. The name is an abbreviation for Bourne-Again SHell. Scripting enables for the execution of instructions that would otherwise be executed o
15 min read
How to Create a Shell Script in linux Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
OSI Security Architecture The OSI Security Architecture is internationally recognized and provides a standardized technique for deploying security measures within an organization. It focuses on three major concepts: security attacks, security mechanisms, and security services, which are critical in protecting data and commun
8 min read
Test Plan - Software Testing Software testing is important to make sure applications work properly and meet user needs. A clear and detailed test plan is the foundation of successful testing, guiding everything from creating test cases to fixing issues. In this article, we will break down what a test plan is, why itâs important
15+ min read
Spring Boot Actuator Developing and managing an application are the two most important aspects of the applicationâs life cycle. It is very important to know what is going on beneath the application. Also, when we push the application into production, managing it gradually becomes critically important. Therefore, it is a
5 min read
Spring - MVC Framework The Spring MVC Framework follows the Model-View-Controller architectural design pattern, which works around the Front Controller, i.e., the Dispatcher Servlet. The Dispatcher Servlet handles and dispatches all incoming HTTP requests to the appropriate controller. It uses @Controller and @RequestMapp
4 min read
Exception Handling in Spring Boot Exception handling in Spring Boot helps deal with errors and exceptions present in APIs, delivering a robust enterprise application. This article covers various ways in which exceptions can be handled and how to return meaningful error responses to the client in a Spring Boot Project. Key Approaches
8 min read