Skip to main content
Quick Reference

React Native Cheatsheet

React Native on one page — views, flex layout, presses, lists, input, platform code, and the traps around Text and flexDirection.

10 sections 10 tables 14 code snippets

Start an app

Expo

npx create-expo-app shelf
npx expo start

The root component

import { View, Text } from "react-native";

export default function App() {
  return (
    <View>
      <Text>Dune</Text>
    </View>
  );
}

The names

NameMeaning
ExpoThe usual start. npx expo start opens Metro and a dev menu
MetroThe bundler. The default port is 8081
HermesThe JavaScript engine, on by default since 0.70
New ArchitectureOn by default since 0.76. Fabric draws the UI. TurboModules talk to native code
Fast RefreshKeeps component state after a save. A full reload resets it
View and TextThe boxes and the words. There is no div and no span

This page is React Native on iOS and Android. A web React component will not render here until you swap the tags.

Core components

A press

import { Pressable, Text } from "react-native";

<Pressable onPress={() => setCount(count + 1)}>
  <Text>Add</Text>
</Pressable>

A picture

<Image
  source={{ uri: "https://example.com/cover.jpg" }}
  style={{ width: 80, height: 120 }}
/>

Which component

ComponentUse it for
ViewLayout. It does not show a raw string
TextEvery string the user reads. Nested Text shares font styles and flows on one line
ImageA remote uri, or require("./cover.png") for a file in the project
PressableA tap. onPress is the handler. onClick is not
TextInputTyped text. value plus onChangeText
ScrollViewA short page that should scroll. It renders every child

Flex layout

Fill the screen

const styles = StyleSheet.create({
  screen: { flex: 1, padding: 16 },
  row: { flexDirection: "row", gap: 8 },
});

Use the styles

<View style={styles.screen}>
  <View style={styles.row}>
    <Text>Dune</Text>
    <Text>412</Text>
  </View>
</View>

Layout rules

PropertyDefault
flexDirectioncolumn. Children stack downward. The web default is row
flexA single number. flex: 1 takes the free space on the parent main axis
flexShrink0. A child keeps its size unless you set flexShrink: 1
justifyContentAlong the main axis. In a column, that is vertical
alignItemsAlong the cross axis. stretch is the default
NumbersDensity-independent pixels. There is no px. "100%" is a string

State

A counter

const [count, setCount] = useState(0);

<Pressable onPress={() => setCount((value) => value + 1)}>
  <Text>{count}</Text>
</Pressable>

Update rules

FactDetail
useStateReturns the current value and a setter. The first render uses the initial value
Updater functionsetCount((value) => value + 1) reads the latest count. Two plain setCount(count + 1) calls share one snapshot
BatchingSeveral setters in one press paint once
New objectAn inline style={{ }} is a new object every render. StyleSheet.create is stable
Props downA parent passes data. A child calls a function prop to talk back
useEffectRuns after paint. Return a function to cancel a subscription

Lists

Only the rows on screen

<FlatList
  data={books}
  keyExtractor={(item) => item.id}
  renderItem={({ item }) => <Text>{item.title}</Text>}
/>

List rules

PieceWhy
FlatListRenders the visible rows. A ScrollView of mapped rows builds every row
dataThe array. Change the array reference when the items change
keyExtractorA stable id. An index key breaks when rows are inserted or reordered
renderItemOne row. Keep the function light. Heavy work belongs outside the row
ListEmptyComponentWhat to show when data is empty
onEndReachedLoad the next page. Guard it so one scroll does not fire the request twice

Text input

A controlled field

<TextInput
  value={title}
  onChangeText={setTitle}
  placeholder="Title"
/>

Keyboard rules

PieceDetail
valueThe state string. The field shows that string
onChangeTextReceives the next string. setTitle can be passed directly
keyboardTypenumber-pad, email-address, and others. The keyboard is native
KeyboardAvoidingViewLifts the field above the keyboard. behavior is often padding on iOS
returnKeyTypeThe label on the return key. onSubmitEditing runs when it is pressed
FocusA field can sit under the keyboard when the screen does not move

Platform

One branch per OS

const top = Platform.OS === "ios" ? 20 : 0;

What differs

APIDetail
Platform.OSios or android. Use it for a real platform difference, not for layout that flex can do
Platform.selectAn object of values, one key per OS, plus default
Safe areaThe core SafeAreaView applies on iOS. react-native-safe-area-context covers Android notches too
Status barStatusBar style and background. Android and iOS do not share one look
ShadowsiOS uses shadow*. Android uses elevation. Set both when you want a shadow
BackAndroid has a system back. iOS uses the header back button and the edge swipe

Navigation

An Expo Router link

import { Link } from "expo-router";

<Link href="/book/dune">Dune</Link>

Screen rules

FactDetail
Not built inReact Native has no router. Expo Router and React Navigation are the usual choices
File routesExpo Router maps a file in app/ to a URL
navigateReact Navigation: navigation.navigate("Book", { id: "dune" })
ParamsPass data that can be serialized. A function in params warns and can be lost
StackPushes a screen. Back pops it
FocusA screen that stays mounted can still be hidden. Refresh in a focus effect, not only on first mount

Native code

A local image

<Image source={require("./cover.png")} />

Where JavaScript stops

PieceDetail
requireThe path must be a static string. A variable path is rejected at bundle time
Expo GoRuns a fixed set of native modules. Your own native code needs a development build
New ArchitectureNew native modules are TurboModules. The old bridge stays only as an interop layer
HermesCompiles the bundle. A syntax error shows in Metro, not as a native crash
PermissionsCamera, location, and photos are native prompts. Asking is a separate API from using the feature
ReleaseThe store build does not include the dev menu. Test the release binary before you ship

Interview traps

Traps worth memorising

TrapWhat happensPrefer
<View>Dune</View>Invalid. A raw string cannot sit in a View<Text>Dune</Text>
Expecting a rowflexDirection defaults to columnflexDirection: "row" when items sit side by side
A wide child overflowingflexShrink defaults to 0, so the child does not shrinkflexShrink: 1 on that child
flex: "1" copied from CSSflex takes one numberflex: 1
ScrollView and books.mapEvery row is mounted, including the ones off screenFlatList
keyExtractor returning the indexRows reuse the wrong state after an insertA stable item.id
setCount(count + 1) twiceBoth calls read the same countThe updater form
require(coverPath)The bundler cannot see a variable pathA literal require("./cover.png")
<Image source="https://..." />The source is not a URL stringsource={{ uri: "https://..." }}
onClickNothing runsonPress
A custom native module in Expo GoThe module is missing from that clientA development build
The core SafeAreaView on AndroidIt does not pad the notch therereact-native-safe-area-context

Text must be wrapped

<View>
  <Text>Dune</Text>
</View>

Side by side

<View style={{ flexDirection: "row" }}>
  <Text>Dune</Text>
  <Text>412</Text>
</View>

How to use this page: start with npx create-expo-app, put every string in Text, and set flex: 1 on the screen. Then render the books with FlatList and handle taps with Pressable.