React Native Basics
Based on the NetNinja Course
Updated: 03 September 2023
From the Net Ninja’s React-Native Course on YouTube
Introduction
React native allows us to use React to develop native mobile applications. We’re going through this course using the Expo CLI along with Typescript instead of just plain RN with JS
Init App
To create a new RN app we use the expo-cli
using the following:
And select the blank (TypeScript)
as the application type. Then, cd
into the created directory and run yarn start
You can then scan the barcode from the terminal via the Expo Go
app which you can download from the app store on your test device
In the scaffolded code you will see the following structure:
Views and Styles
The App.tsx
file contains a View
with some Text
and a StyleSheet
App.tsx
In RN we use the Text
component to represent any plain text view as we would in typical JSX
, additionally, the View
can be likened to a div
and the StyleSheet
to a CSS File
In terms of the way RN uses styles it uses a CSS-like API but doesn’t actually render to CSS but does magic in the background to get these to be like native styles
An important distinction when compared CSS is that RN styles aren’t inherited by children elements, this means that if we want to apply a specific font to each component for example, we would actually have to apply this to every element individually
Text Inputs
RN Provides some other basic components, one of these are the TextInput
component which we can use like so:
Lists and ScrollView
ScrollView
allows us to render a list of items in a scrollable area, this can be rendered as follows:
FlatList
Additionally, there’s another way to render a scrollable list of elements: using a FlatList
which looks like so:
Additionally, FlatList
also supports a numColumns
prop which lets us set the number of columns to use for the layout
The FlatList
will only render the components as they move into view, and this is useful when we have a really long list of items that we want to render
Pressable
We can use the Pressable
component to make any element able to handle touch events, for example:
In general, we can surround an element by Pressable
and then handle the onPress
event:
And then, somewhere higher up we can have onPersonSelected
defined:
We can set some app props by applying this to the FlatList
components above.
Without too much detail, some of the types being referenced by our setup are:
And our person display component will then look like this:
Keyboard Dismissing
We’ll notice that the way the app works at the moment, some basic interactions, such as the keyboard automatically being dismissed don’t work. In order to do this we need to add a handler onto the component which causes the keyboard t be dismissed.
This can be done using a combination of a Pressable
or TouchableWithoutFeedback
and then Keyboard.dismiss()
in the onPress
handler
Flexbox
Just a short note here - Views
in RN behave like flexboxes, so we can do normal flexboxy things for layout in our components.
Something that’s commonly done is to use flex: 1
on components so that they fill the available space on a page. This is especially useful for limiting the size of a ScrollView
or FlatList
Additionally, applying flex: 1
to the root component will cause the app to fill our entire screen
Fonts
To use custom fonts you can make use of the Expo CDK, here’s the doc
And we can use a font from our app with the useFonts
hook:
React Navigation
We’re going to use the react-navigation
library for building out the navigation and routing.
We can to install the required packages with:
expo install
does some additional version checks, etc.
Additionally, react-navigation
supports a few different types of navigation. For our first case we’re going to use a Stack navigation. So we need to install that with:
Stack Navigator
We can create a Stack Navigator using some of the constructs provided by react-navigation
. These work by using createStackNavigator
and composing the navigation within a NavigationContainer
. Note that we also use the AppStackParamList
to list the params required for each screen
routes/HomeNavigationContainer.tsx
We can then use this from the App
component with:
App.tsx
Navigating
In order to navigate we need to use the navigation
prop that’s passed to our component by react-navigation
. In order to do this we need to configure our screen to use the AppStackParamList
type with StackScreenProps
screens/Home.tsx
Send data to screen
We can send some data to each screen by defining the data in our routing params:
And then our Reviews screen will look like this:
And lastly, we can update the Home
component to send the data that this screen requires using the navigator.navigate
function:
Drawer Navigation
Now, since we’ve got all our navigation working, we’re going to add some complexity by including a drawer based navigation that wraps our overall application. To do this we will need to change when we’re using our stack and creating the NavigationContainer
To do this, we’ll first return just the stack from the HomeNavigationContainer
and then create the container at the App
component level. It also may be useful to rename the HomeNavigationContainer
file to HomeStack.tsx
routes/HomeStack.tsx
Next, we need to add the About
page content into a stack, like so:
routes/AboutStack.tsx
Then, we will include these components/stacks as the component that needs to be rendered. Since we’re using a DrawerNavigator
, we do this by first creating the navigator, then providind our screens as the routes:
routes/DrawerNavigator.tsx
And lastly, we can use this from the App.tsx
file like so:
App.tsx
Now that we’ve got the drawer working, it’s a matter of finding a way to trigger it from our code, we can use a custom header component to do this. A special consideration here is that this component should have a definition for a composite navigation type in order to access and work with the drawer:
We can then implement the Header
in the HomeStack
component in the options
param for the stack:
routes/HomeStack.tsx
And the exact same for About
routes/AboutStack.tsx
Custom Title for Stack-Generate Header
Using the Stack Navigation, we are also provided with a title, this is still used by the ReviewDetails
screen as it doesnt have any additional options. In addition to the options
param as an object, we can also set it to a function which calculates the values from the data shared to the component. So we can use the title
prop from our component to set this:
routes/HomeStack.tsx
Images
To use images in React-Native, we make use of the Image
component with the source
prop with a require
call
Note that the require
data must be a constant string, so we can’t calculate this on the fly
If we have a set of images that we would like to dynamically select from, we can however still do something like this:
Additionally, if we want to call an image from a URL, we can do this as we normally would, for example:
BackgroundImage
In RN we can’t set image backgrounds using a normal background
style prop, instead we need to use a BackgroundImage
component from react-native
, this works similar to the Image
component but with the element we’re adding the background to contained in it
Modals
RN comes with a handy modal component which allows us to render modals, Modal
s are controlled using a visible
prop. A modal in use could look something like this:
screens/About.tsx