A quick reference guide for React Native, covering fundamental aspects of developing mobile applications using this framework.
|
Expo
|
For beginners
Simplifies the setup process Provides OTA updates Does not allow you to add custom native code Expo apps tend to have larger sizes |
|
React Native CLI
|
For Experienced Developers
Supports integrating custom native modules Potentially better performance for complex applications Requires Xcode or Android Studio to get started. No OTA updates. |
|
Initialize a new project
|
npx create-expo-app my-app |
|
Start development server
|
cd my-app npm start |
|
Android
|
Use the Expo Go app to scan the QR code from your terminal to open your project.
|
|
iPhone
|
Use the built-in QR code scanner of the default iOS Camera app.
|
|
Expo
|
A set of tools and services to make development with React Native easier.
|
|
Expo SDK
|
A modular set of packages that provides access that provides access to native APIs, like Camera or Notifications.
|
|
Expo CLI
|
A command-line tool that is the primary interface between a developer and other Expo tools.
|
|
Expo Go
|
An open-source sandbox app you can download on your phone to view your app in development.
|
|
Expo Snack
|
A web-based playground where you can write React Native snippets and run them in the browser.
|
|
Expo Tunnel
|
For establishing a connection that allows devices to access the app even if they're not on the same wireless network.
npx expo start --tunnel |
|
An abstraction similar to CSS StyleSheets.
Declare styles in a structured and optimized manner. You can use an array of styles to combine multiple style objects- the last style in the array has precedence, or mix predefined styles with inline styles. All of the core components accept a prop named style. |
|
import React from 'react'; |
|
Provides and subscribes to color scheme updates from the appearance module in react native.
It returns the current color scheme preference of the user's device. Supported color schemes: "light", "dark", null. |
|
import React from 'react'; |
|
Used to get the dimensions of the device window.
It returns an object containing the window's width and height. Useful for creating responsive designs and layouts that adapt to different screen sizes. |
|
import React from 'react'; |
|
A basic button component that should render on any platform.
Supports a minimal level of customization. |
|
import React from 'react'; |
|
|
Required props: title and onPress
|
|
Used for users press interactions.
Detects various stages of press interactions on any of its child components. Highly customizable and flexible way to handle touch-based input. Inherits all the styles of the View component. |
|
import React from 'react'; |
|
|
props:
onPressIn: method called when a press is activated. onPressOut: method called when the press gesture is deactivated. onLongPress: method called when user leaves their finger longer than 500 milliseconds before removing it, customize this time period using the delayLongPress prop. pressed: state that refers to a boolean value provided to the style and children functions of Pressable, to check if component is being pressed. hitSlop: prop to increase the area where touch gestures are recognized. (extended interactive area "HitRect"). pressRetentionOffset: prop to specify the area in which the touch can move while maintaining the press's active state. ("PressRect"). |
|
React Navigation
|
|
React Native does not come with built-in navigation capabilities.
React Navigation is the most popular third-party library. Enable developers to implement various navigation patterns. Provides a set of navigators, such as stack, tab, and drawer navigators. |
|
Allows transition between screens where each new screen is placed on top of a stack.
|
|
NavigationContainer: Component container for your app's navigation structure.
Manages the navigation tree and contains the navigation state. Should be only used once in your app at the root. |
|
createNativeStackNavigator: Function that returns an object containing two properties.
Navigator: Takes Screen elements as its children to define the configuration for routes. initialRouteName: prop for the Navigator specify what the initial route in a stack is. screenOptions: prop to Navigator to specify common options. Screen: Component takes 2 required props name and component. name: prop which refers to the name of the route. component: prop which specifies the component to render for the route. options: prop to Screen to specify screen-specific options. |
|
navigation and route props: are automatically provided to each screen component by the navigator.
navigation: prop is available to all screen components and allows you to control navigation actions. route: prop contains information about the route, including parameters passed to that screen. You can read the params through route.params inside a screen. Params should contain the minimal data required to show a screen. |
import * as React from 'react'; |
|
Navigation actions:
navigation.navigate('RouteName'): Pushes a new route to the native stack navigator if it's not already in the stack. If you navigate to a route that is not defined in the navigator, it will print an error in the development mode and will not show errors in production mode. navigation.push('RouteName'): Used to navigate to a screen in the stack navigator, adding a new route to the navigation regardless of the existing navigation history. navigation.goBack(): Is used to programmatically go back to the previous screen. navigation.popToTop(): Used to go back to the first screen in the stack. |
|
Renders a navigation drawer on the side of the screen which can be opened and closed via gestures.
|
|
You cannot use the useNavigation hook inside the drawerContent since useNavigation is only available inside screens. You get a navigation prop for your drawerContent which you can use instead.
|
|
drawerPosition: prop typically set in the screenOptions to specify the position of the drawer, such as left or right.
|
|
drawerContent: prop in the Drawer Navigator that allows you to provide a custom component for the drawer's content.
|
|
CustomDrawerContent: refer to a user-defined React component that is passed to the drawerContent prop.
|
|
DrawerItem: in a custom drawer allows for more flexibility and customization compared to defining routes directly in the navigator.
|
import * as React from 'react'; |
|
The following are also available:
navigation. jumpTo('RouteName'): go to a specific screen in the drawer navigator. navigation. openDrawer: open the drawer. navigation.closeDrawer: close the drawer. navigation.toggleDrawer: toggle the state, ie. switch from closed to open and vice versa. |
|
Common style of navigation.
|
|
Can be tabs on the bottom of the screen or on the top below the header.
|
|
Bottom tab navigation: A simple tab bar on the bottom of the screen that lets you switch between different routes.
|
|
Routes are lazily initialized -- their screen components are not mounted until they are first focused.
|
|
You cannot use the useNavigation hook inside the tabBar since useNavigation is only available inside screens. You get a navigation prop for your tabBar which you can use instead.
|
import React from 'react'; |
|
The following are also available:
navigation.jumpTo('RouteName'): is a method that directly switches to a specified screen within the tab navigator. |
|
A container that supports layout with flexbox, style, some touch handling, and accessibility controls.
Like a <div> in HTML. Designed to be nested inside other views and can have 0 to many children of any type. |
import React from 'react'; |
|
A component for displaying text.
Supports nesting, styling, and touch handling. Everything inside it is no longer using the Flexbox layout but using text layout. Elements inside it are no longer rectangles, but wrap at the end of the line. |
|
import React from 'react'; |
|
|
You must wrap all the text nodes inside of a <Text> component
|
|
|
Will raise exception
<View> Some text </View> |
|
|
Correct
<View> |
|
|
Text container: Text will be inline if the space allow it, otherwise, text will flow as if it was one.
<Text>First part and second part |
|
|
View container:
Each text is its own block, otherwise, the text will flow in its own block. <View>First part and second part |
|
Creates a scrollable area when content exceeds screen's physical limits.
Can contain multiple components and views. Can be scrolled vertically or horizontally. Must have a bounded height in order to work. Renders all its react child components at once. |
|
import React from 'react'; |
|
Used to efficiently render long lists.
Offers features like pull-to-refresh, infinite scrolling, and easy item separators. Lazy rendering: renders items only when they appear on the screen and are removed when the user scrolls away from them. Internal state is not preserved when content scrolls out of the render window. Inherits the props of the ScrollView component. |
|
import React from 'react'; |
|
|
Two required props:
data: accepts a plain array that contains the list of items to display. renderItem: a function that goes over each item in the array and renders it into the list. keyExtractor: It instructs the list to use the id of each item as React keys instead of the default key property. |
|
Used for rendering large lists with section headers.
Uses lazy rendering to achieve faster rendering. Inherits the props of the ScrollView component. Internal state is not preserved when content scrolls out of the render window. Provides support for section headers and section separators. |
|
import React from 'react'; |
|
|
Two required props:
sections : accepts the array that contains the list of items to display, akin to the data prop in FlatList. renderItem: method which acts as the default renderer for every item in each section. renderSectionHeader: prop, render each section’s header. |
|
Used for inputting text into the app via a keyboard.
|
|
import React, { useState } from 'react'; |
|
Used for displaying different types of images,
network images, static resources, temporary local images, and images from local disk, such as the camera roll. You can also add style to an image. |
|
import React from 'react'; |
|
|
resizeMode :
'cover': Scales image to fill the container, maintaining its aspect ratio. 'contain': Scales image to fit inside the container, maintain the image's aspect ratio ensuring the entire image is visible. 'stretch': Stretches image to fill the container, possibly distorting the aspect ratio. 'center': Centers image in the container without scaling. 'repeat': Repeats the image to cover the container. |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.