Setting Up An App
Project Generation
To generate a boilerplate project, you have 2 popular CLIs available:
expo-cli
react-native-cli
The expo-cli
project generator comes with a lot more useful features pre-configured, so you'll generally prefer this. (Examples include icons, video, camera use, etc.)
More on React Navigation
Types of navigators
There are 3 kinds of navigators that are commonly used:
DrawerNavigator
BottomTabNavigator
StackNavigator
Each of these navigators are like big objects that store the different views that can be rendered.
Configuring App.js
App.js
The App.js
file is a special file where the default export is the root location from which React Native renders your entire application.
When you use react-navigation
(and its associated libraries), 3 main things tend to happen in your App.js
file:
You create the
navigator
using one of the navigator types, passing in an object containing your screens.In that same
navigator
initialization, you pass configuration for that navigator.You export the
navigator
wrapped aroundcreateAppContainer
. (This happens because React Native expects a React component to be exported fromApp.js
, and thenavigator
by itself is not a React component.)
Simple example:
Displaying Icons
Expo tends to give you a very useful library called @expo/vector-icons
. This is a collection of React icon libraries like Ant Design, Font Awesome, etc. that you can use.
onEndEditing
in TextInput
onEndEditing
in TextInput
When a user is done adding text to an input and presses enter or submit, you use the onEndEditing
prop to trigger a callback. This callback gets passed an event object.
Styling Tips
Displaying images
By default, even if you give a primitive Image
component a source
prop, it won't appear on the page until you give the Image
height and width dimensions.
Displaying content that goes beyond the screen
If your content gets long enough, it will go beyond the screen. By default, you can't get to that content.
There are 3 potential fixes to this:
Apply
flex: 1
to the parent container, so it stretches vertically as far as possible.Use a
ScrollView
component to add scroll-ability.Remove the parent container altogether and just use a
React.Fragment
, so you don't have to style any parent container.
Last updated