--- title: Build a screen --- import ImageSpotlight from '~/components/plugins/ImageSpotlight'; import { SnackInline, Terminal } from '~/ui/components/Snippet'; import { A } from '~/ui/components/Text'; import Video from '~/components/plugins/Video'; import { Step } from '~/ui/components/Step'; import { BoxLink } from '~/ui/components/BoxLink'; import { BookOpen02Icon } from '@expo/styleguide-icons'; In this chapter, we will create the first screen of the StickerSmash app. The screen above displays an image and two buttons. The app user can select an image using one of the two buttons. The first button allows the user to select an image from their device. The second button allows the user to continue with a default image provided by the app. Once the user selects an image, they'll be able to select and add a sticker to the image. So, let's get started creating this screen. ## Break down the screen Before we build this screen by writing code, let's break it down into some essential elements. Most of these elements directly correspond to the built-in [Core Components](https://reactnative.dev/docs/components-and-apis) from React Native. There are three essential elements: - The screen has a background color - There is a large image displayed at the center of the screen - There are two buttons in the bottom half of the screen The first button is composed of multiple components. The parent element provides a yellow border and contains an icon and text components inside a row. In React Native, styling (such as the yellow border) is done using JavaScript as compared to the web, where CSS is used. Most of the React Native core components accept a `style` prop that accepts a JavaScript object as its value. For detailed information on styling, see [Styling in React Native](https://reactnative.dev/docs/style). Now that we've broken down the UI into smaller chunks, we're ready to start coding. ## Style the background Let's change the background color. This value is defined in the `styles` object in the **App.js** file. Replace the default value of `#fff` with `#25292e` for the `styles.container.backgroundColor` property. It will change the background color of the screen. {/* prettier-ignore */} ```jsx App.js import { StatusBar } from 'expo-status-bar'; import { StyleSheet, Text, View } from 'react-native'; export default function App() { return ( Open up App.js to start working on your app! ); } const styles = StyleSheet.create({ container: { flex: 1, /* @info Replace the default value of backgroundColor property with '#25292e'. */ backgroundColor: '#25292e', /* @end */ alignItems: 'center', justifyContent: 'center', }, }); ``` > React Native uses the same color format as the web. It supports hex triplets (this is what `#fff` is), `rgba`, `hsl`, and a set of named colors like `red`, `green`, `blue`, `peru`, and `papayawhip`. For more information, see [Colors in React Native](https://reactnative.dev/docs/colors). ## Change the text color The background is dark, so the text is difficult to read. The `` component uses `#000` (black) as its default color. Let's add a style to the `` component to change the text color to `#fff` (white) in **App.js**. {/* prettier-ignore */} ```jsx import { StatusBar } from 'expo-status-bar'; import { StyleSheet, Text, View } from 'react-native'; export default function App() { return ( /* @info Replace the default value of color property to '#fff'. */ Open up App.js to start working on your app! /* @end */ ); } /* @hide const styles = StyleSheet.create({*/ const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#25292e', alignItems: 'center', justifyContent: 'center', }, }); /* @end */ ``` ## Display the image We can use React Native's `` component to display the image in the app. The `` component requires a source of an image. This source can be a [static asset](https://reactnative.dev/docs/images#static-image-resources) or a URL. For example, the source can be required from the app's **./assets/images** directory, or the source can come from the [Network](https://reactnative.dev/docs/images#network-images) in the form of a `uri` property. Next, import and use the `` component from React Native and `background-image.png` in the **App.js**. Let's also add styles to display the image. {/* prettier-ignore */} ```jsx import { StatusBar } from 'expo-status-bar'; import { StyleSheet, View, /* @info Import the image component. */Image/* @end */ } from 'react-native'; /* @info Import the image from the "/assets/images" directory. Since this picture is a static resource, you have to reference it using "require". */ const PlaceholderImage = require('./assets/images/background-image.png'); /* @end */ export default function App() { return ( /* @info Wrap the Image component inside a container. Also, add the image component to display the placeholder image. */ /* @end */ ); } const styles = StyleSheet.create({ container: { /* @info Modify container styles to remove justifyContent property. */ flex: 1, backgroundColor: '#25292e', alignItems: 'center', /* @end */ }, /* @info Add styles for the image. */ imageContainer: { flex: 1, paddingTop: 58, }, image: { width: 320, height: 440, borderRadius: 18, }, /* @end */ }); ``` The `PlaceholderImage` variable references the **./assets/images/background-image.png** and is used as the `source` prop on the `` component. ## Dividing components into files As we add more components to this screen, let's divide the code into multiple files: - Create a **components** directory at the root of the project. This will contain all the custom components created throughout this tutorial. - Then, create a new file called **ImageViewer.js**, inside the **components** folder. - Move the code to display the image in this file along with the `image` styles. {/* prettier-ignore */} ```jsx ImageViewer.js import { StyleSheet, Image } from 'react-native'; export default function ImageViewer({ placeholderImageSource }) { return ( ); } const styles = StyleSheet.create({ image: { width: 320, height: 440, borderRadius: 18, }, }); ``` Next, let's import this component and use it in the **App.js**: {/* prettier-ignore */} ```jsx App.js import { StatusBar } from 'expo-status-bar'; import { StyleSheet, View } from 'react-native'; /* @info */ import ImageViewer from './components/ImageViewer'; /* @end */ const PlaceholderImage = require('./assets/images/background-image.png'); export default function App() { return ( /* @info Replace Image component with ImageViewer */ /* @end */ ); } /* @hide const styles = StyleSheet.create({ */ const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#25292e', alignItems: 'center', }, imageContainer: { flex: 1, paddingTop: 58, }, }); /* @end */ ``` ## Create buttons using `Pressable` React Native provides various components to handle touch events on native platforms. For this tutorial, we'll use the [``](https://reactnative.dev/docs/pressable) component. It is a core component wrapper that can detect various stages of interactions, from basic single-tap events to advanced events such as a long press. In the design, there are two buttons we need to implement. Each has different styles and labels. Let's start by creating a component that can be reused to create the two buttons. Create a new file called **Button.js** inside the **components** directory with the following code: {/* prettier-ignore */} ```jsx Button.js import { StyleSheet, View, Pressable, Text } from 'react-native'; export default function Button({ label }) { return ( alert('You pressed a button.')}> {label} ); } const styles = StyleSheet.create({ buttonContainer: { width: 320, height: 68, marginHorizontal: 20, alignItems: 'center', justifyContent: 'center', padding: 3, }, button: { borderRadius: 10, width: '100%', height: '100%', alignItems: 'center', justifyContent: 'center', flexDirection: 'row', }, buttonIcon: { paddingRight: 8, }, buttonLabel: { color: '#fff', fontSize: 16, }, }); ``` Now, in the app, an alert will be displayed when the user taps any of the buttons on the screen. It happens because the `` calls an `alert()` in its `onPress` prop. Let's import this component into **App.js** file and add styles for `` component that encapsulates these buttons: {/* prettier-ignore */} ```jsx App.js import { StatusBar } from "expo-status-bar"; import { StyleSheet, View} from "react-native"; /* @info */import Button from './components/Button'; /* @end */ import ImageViewer from './components/ImageViewer'; const PlaceholderImage = require("./assets/images/background-image.png"); export default function App() { return ( /* @info Use the reusable Button component to create two buttons and encapsulate them inside a View component. */