React Native II Flashcards
What are we going to learn in React Native II?
Mosh worked really hard on this course
Promise you’ll be a React-Native Ninja
Tons of Tips and Tricks
If we build an App or get a job, share story!
Advanced Concepts:
Native Device features (Android/iOS)
Navigation
Networking (Api calls)
Offline support
Authentication & Authorization
Push Notifications
Distrubution (App stores)
What do we need to know?
Ultimate React Native Part I
React Hooks
(useState, useEffect, custom hooks)
React Context
Javascript
(arrow fns, spread, async/await)
What native features are not available in React Native?
React Native has limited API for working with native features:
Camera, context not available
available as 3rd party library
Ex. image picker -> allows us to get an image from camera
have to install a library
run in iOS project
can eject from expo
or
use components by expo
lots of native features available
What is the ImagePicker component?
Can select a video or image from user’s library or camera
expo install expo-image-picker
What is useEffect (effect hook) for?
accessing lifecycle hooks like componentDidMount ( ) in functional component
How can we access a user’s camera?
ImagePicker component from ‘expo-image-picker’
What is the permissions API?
another method for accessing camera, etc.
can pass multiple permission types
How can we access a user’s library to add a photo and display on the screen?
useState() hook
to set result.uri (from ImagePicker)
display imageUri in Image component
How do we build a re-usable ImageInput component?
How do we handle the touch event of our image picker component?
consumer of ImageInput:
handles state using hooks
ImageInput:
props - imageUri, onChangeInput (reference to hook from consumer for updating data of imageUri)
wrap in a TouchableWithoutFeedback
set handlePress to fn for calling ImagePicker.launchImageLibrary
How do we build the image input list component?
How do we solve this problem?
Hint: Horizontal scrolling
ScrollView component from react-native
wrap ScrollView in a View so it only takes up content space
use useRef() hook to get current element and scroll to end
How do we design a form image picker?
wrap our ImageInputList in a new component with an error message
Just like FormPicker, FormField
allow Formik to handle state
use the values field to get form values
use the SetFieldValue method to update form values
How do we add the Form Image Picker to our Listing Edit Screen?
Consume FormImagePicker in ListingEditScreen
add validation to images using Yup schema in ListingEditScreen
How do we get the user’s location?
define function:
getLocation( ) to async call Location.getLastKnownPosition()
useEffect :
to call getLocation( )
useState:
set location and get location
What is a custom hook?
Encapsulate some state and logic around state
in a re-usable component
naming convention:
useLocation
*start with “use”
What are we going to learn in navigation?
How to take App to next level
add navigation at bottom
can click on screens
App is becoming more functional
How do we implement navigation in our React Native Apps?
What kind of navigators do we have in React?
Stack - taking users from one page to another
Tab - going between tabs
Drawer - drawing things on screen navigations
**all of them are very similar and can learn if you know one!
How do we use the stack navigator?
Each navigator is implemented in a separate library
We render StackNavigatorComponent
it knows how to navigate between components
What does React Navigation give our screen components?
A special prop called navigation
can destructure and pick navigation property
Ex. navigation.navigate(“Target Location”)
only available in screen component
not available in child components
.push creates a new instance on the stack
.navigate creates a single instance on stack
but can use navigation hook to access navigation object
How do we access navigation prop using React Navigator hook?
useNavigation( ) hook
How do we pass parameters to React Navigator?
ex. pass ID of a specific tweet
How can we set the title of a screen?
using the options prop of the Stack.Screen component
How do we customize the header of a screen?
Locally (screen only)
use the options prop of the Stack.Screen component
headerStyle (pass a color)
headerTintColor
headerShown
Globally (all screens)
options prop of the Stack.Navigator component
can override for specific screen
How do we create a bottom tab navigator?
npm install @react-navigation/bottom-tabs
createBottomTabNavigator()
returns an object
has Screen and Navigator properties
render
TabNavigator inside NavigationContainer
How do we add a background color to each tab and change the text when pressed?
createBottomTabNavigator()
tabBarOptions = styles
/>
How can we add icons to our Tabs?
Really easy
Tab.Screen
options - set to a fn that renders a MaterialCommunityIcon
destructure the props from React Navigator
set size and color to props given by Navigator
What is nesting navigators?
instead of passing a component to the Tab.Screen component (bottomTabNavigator)
passing a Stack Navigator
called nesting navigators
How do we build the welcome screen to login screen / register screen navigation?
AuthNavigator.js
use createStackNavigator( ) from @react-navigation/stack
from the return object
create Screen component and Navigator container
Consuming Screen:
inherits navigation prop
uses navigate method onPress of button
How do we set a custom theme for our app?
spread the …DefaultTheme
can update headers in Stack.Screen
pass options prop
headerStyle: { backgroundColor: ‘white’ }
How do we create the AppNavigator component?
How do we create navigation from the listings screen to press on an item and be directed to the item details screen?
add TouchableWithoutFeedback to card component
assign the onPress event via props to consuming class
because we have registered the screen with our navigator….
can access the navigation or route props
handle onPress in ListingScreen by extracting navigation prop (ListingsScreen)
extract route param, use it to get item
listing.title, listing.price
dynamically display item
How can we navigate to an item dynamically?
wrap card component in a Touchable
set the event to props (onPress)
handle in ListingsScreen (navigation prop)
handle ListingDetailsScreen (route to get item[param])
How do we make our listing details screen appear from the bottom vs from the side?
Set Stack.Navigator prop to modal
What does our account screen need?
It’s own stack navigator for navigating to it’s components
How do we make our AppNavigator tabs beautiful?
create NewListingButton component
let consumer decide what should happen when onPress event
hook with prop
What should we do to make our navigation cleaner?
Get ride of hard coded route names
if there is a typo, app will not work
testing every feature manually or automated
if rename route, have to rename all instances
solns?
store all routes in a single place in app
Object.freeze ensures object cannnot be modified anywhere else in our app
What are we going to learn in the networking section?
How to connect our React-Native app to a node.js server
created a fake database
Why?
Don’t need to setup MongoDB, etc.
What library are we going to use for calling APIs?
Hint: npm i apisauce
Api Sauce
an abstraction over axios
can use fetch API, but using api sauce
why?
whenever call server, promise always resolved, even if an error
just check if there is an error
no try/catch needed
gives us standardized errors
What should our components know?
What UI looks like
How UI behaves
Why?
separation of concerns
NOT:
HTTP protocol
calling GET, POST, ETC.
not responsibility of component
Don’t do this, violates separation of concerns
Soln?
Create an API layer that handles all this
proper architecture
How do we create our basic API layer?
How can we get the list of items from our backend?
How do we debug our API calls to see if the server was called as well as the request and response objects?
Look at network requests in React Native Debugger
How should we handle an error?
should provide a good user experience
Not a blank screen
How?
use another hook to catch an error from APISauce
conditionally render the error with a Text component
and a Button component
How do we simulate a slow network connection?
React native developer tools
makes our app slow
takes 2 seconds of a blank screen before listings
soln?
Show an animation before listings shown
How do we display an activity indicator?
ActivityIndicator component
defined in react native core
use a separate hook for loading, setLoading
render component if loading is true
kind of boring can replace with a beautiful animation
What is Lottie?
an animation library built by AirBnB
Can add beautiful animations
JSON file exported from Adobe After Effects
LottieFiles.com
thousands of animations made by people
most free, some paid
How can we add a beautiful Lottie animation to our application?
Hint: expo install lottie-react-native
Go to LottieFiles.com
customize a free animation
download the JSON
upload into project
render using a special component
What’s the problem with this implementation?
have to repeat logic everytime want to call server
declare hooks, etc.
is there a better way?
repetative, redundant
Soln?
use hooks
encapsulate some state and logic around
can encapsulate details around a re-usable hook
How do we create a re-useable API hook?
extract the useState (state)
extract the logic that modifies state