React Native

the React you know, on your phone

$ npx create-expo-app@latest

Hands up

  • Who was here for the React session?
  • Who has Expo Go on their phone?
  • Who has an iPhone? An Android?
  • Who has built a mobile app before?

What is React Native?

  • JavaScript framework
  • Created by Facebook (now Meta)
  • Used for building apps for iOS and Android
  • Same React: components, props, state, hooks
  • One codebase, two platforms
  • Real native views — not a website in a box

React so far

  • Components are functions that return JSX
  • Props go in, read-only
  • useState — set it, and the component draws again
  • Replace, don't mutate
  • Data flows down, events flow up
  • useEffect for the outside world

All of it still works.

So what is different?

<div><View>
<p>, <span>, <h1><Text>
<img><Image>
<button>, onClick<Pressable>, onPress
<input><TextInput>
<ul> + map<FlatList>
a .css fileStyleSheet.create

The tags change. The React doesn't.

How it works

your components JavaScript · JSX React Native turns <View> into… iOS · UIView Android · View

No browser. No HTML. No CSS.

Oh Wait…

npm

react-native init

expo

Getting started

  • Install Expo Go on your phone
  • Scan the QR code — Android: in Expo Go. iPhone: the Camera app.
  • Same Wi-Fi as your laptop — or npx expo start --tunnel
  • No phone? snack.expo.dev

Every React Native app ever…

DEMO TIME

Hello, phone

  • npx expo start, scan, it opens on the phone
  • Change the text, save — the phone updates by itself
  • Break it on purpose — read the red screen together
  • Shake the phone (or press m) — the dev menu
  • console.log — shows up in the terminal

Core components

Viewa box: layout, background, border
Textevery piece of text, without exception
Imagefrom a file, or a URL
ScrollViewa View that scrolls — nothing scrolls by default
TextInputthe keyboard comes with it
Pressableanything you can tap
FlatListlong lists, done efficiently

All imported from 'react-native'.

All text lives in <Text>

✗ crashes
✓ works

Text strings must be rendered within a <Text> component.

Styling

  • camelCase, like inline styles on the web
  • Numbers, not px: they scale with the screen
  • No cascade: a View's style doesn't reach the Text inside it

Flexbox, but column first

1
2
3
container: {
  flexDirection: 'column',
  justifyContent: 'flex-start',
  alignItems: 'stretch',
}

Every View is already a flexbox, and it stacks down.

Questions

Pressable

onPress, not onClick. The rest is last session's counter.

TextInput

onChangeText hands you the string. No e.target.value.

Lists

  • Scrolls by itself
  • Only draws the rows that are on screen
  • keyExtractor is last session's key

iOS or Android?

Or two files: Card.ios.js and Card.android.js — the right one is picked for you.

More than one screen

$ tree app

_layout.js   ← the stack
index.js     ← /
settings.js ← /settings
todo/
  [id].js   ← /todo/42

Expo Router: a file is a screen.

The phone's own stuff

expo-hapticsa buzz under the finger
expo-camerathe camera, and QR scanning
expo-locationwhere you are
expo-notificationslocal and push notifications

Questions

Exercise: a todo list

Last session's, this time on your phone.

  1. A new Expo app (--template blank)
  2. Bring last session's state: add and toggle done
  3. Swap the tags: View, Text, TextInput, Pressable
  4. A FlatList for the todos
  5. StyleSheet — make it look like an app
  6. Stretch: a buzz when one is done, onLongPress to delete, keep the list with AsyncStorage

Shipping it

  • Expo Go is for learning — your app runs inside someone else's
  • A development build is your own app, with any native library
  • eas build builds it in the cloud — no Mac needed for iOS
  • eas submit sends it to the stores

The stores cost money: Apple yearly, Google once.

What’s next?

Screens and tabsExpo Router — the default template
TypesTypeScript — also the default template
Server dataTanStack Query, same as on the web
Animationreact-native-reanimated
Librariesreactnative.directory

Who is your best friend?

(besides Google, StackOverflow and ChatGPT)

docs.expo.dev

Hungry for more React Native?

expo tutorial the official one, a whole app step by step
reactnative.dev every core component, with live examples
snack.expo.dev React Native in the browser, nothing to install
reactnative.directory which libraries work, and with Expo Go
react.dev/learn still the place for the React part

Questions?

$ open

hackenshaw.com/wip/react-native/

These slides.

$ thanks for coming