2.react Native Components and Props
2.react Native Components and Props
2.react Native Components and Props
COMPONENTS
Dieudonne U
• Components let you split the UI into independent, reusable pieces, and
think about each piece in isolation.
• Conceptually, components are like JavaScript functions. They accept
arbitrary inputs (called “props”) and return React elements describing
what should appear on the screen.
• In React Native, there are two main types of components that make
up an application: functional components and class components.
These are structured the same as they would be in a regular React
app for the web.
Class Components
• Class components are JavaScript ES2015 classes that extend a base
class from React called Component.
import React, { Component } from 'react';
import { Text } from 'react-native';
This gives the class App access to the React lifecycle methods like render as well
as state/props functionality from the parent.
Functional Components
• Functional components are simpler. They don’t manage their own state or have
access to the lifecycle methods provided by React Native. They are literally
plain old JavaScript functions, and are sometimes called stateless components.
List Views
• Flat List
• SectionList
Android Components and APIs
• Many of the following components provide wrappers for commonly
used Android classes.
• BackHandler
• DrawerLayoutAndroid
• PermissionsAndroid
• ToastAndroid
• If you want to render a set of data broken into logical sections, maybe
with section headers, similar to UITableViews on iOS, then a
SectionList is the way to go.
const DATA = [
{
title: "Main dishes",
data: ["Pizza", "Burger", "Risotto"]
}, {
title: "Desserts",
data: ["Cheese Cake", "Ice Cream"]
}
];
<SectionList
sections={DATA}
keyExtractor={(item, index) => item + index}
renderItem={({ item }) => <Item title={item} />}
renderSectionHeader={({ section: { title } }) => (
<Text style={styles.header}>{title}</Text>
)}
/>
Activity Indicator
• Displays a circular loading indicator.
<View style={[styles.container, styles.horizontal]}>
<ActivityIndicator />
<ActivityIndicator size="large" />
<ActivityIndicator size="small" color="#0000ff" />
<ActivityIndicator size="large" color="#00ff00" />
</View>
Modal
• The Modal component is a basic way to present content above an
enclosing view.
Props in React native
• Most components can be customized when they are created, with
different parameters. These created parameters are called props,
short for properties.
• For example, one basic React Native component is the Image. When
you create an image, you can use a prop named source to control
what image it shows.
• Props are immutable
Example
import React from 'react';
import { Image } from 'react-native';
• Think of your container as a line of people. And you are telling each
person to stand 5 meters behind the person in front of him
(marginTop: 5). If this person is set to relative he will respect the line
and will position himself relatively to the person in front of him. If this
person is set to absolute he will ignore all of the people in the line and
will position himself as if the line was empty, 5 meters from where the
line (his parent container) starts.
• The default value is relative
Z-Index
• You can control which components display on top of others. In the
following example the zIndex of the yellow square to 1.
Lifecycle of React Native Application
Component
Introduction
• Lifecycle methods are inbuilt methods. As a Mobile Application
developer, we have to take care of the Lifecycle of each
screen/activity/component because sometimes we have to load the
data accordingly. For example, if I want to initialize some connection
when the user opens the screen reader and closes it when the user
closes it; In both cases we have to have some knowledge about the
Lifecycle methods so that we can perform such actions.
The lifecycle of React Native Application
There are 4 types of Lifecycle methods available in React Native
1. Mounting methods
1. constructor()
2. componentWillMount() (Deprecated after RN 0.60)
3. render()
4. componentDidMount()
2.Updating methods
1. componentWillReceiveProps() (Deprecated after RN 0.60)
2. shouldComponentUpdate()
3. componentWillUpdate() (Deprecated after RN 0.60)
4. componentDidUpdate()
3.Unmounting methods
1. componentWillUnmount()
4.Error handling methods
1. componentDidCatch()
Description
1. Mounting Methods
constructor(): It is the first method called when we open a
screen, it is mostly used to create States.
constructor() {
super();
console.log('Constructor Called.');
}
2. componentWillMount():
It is called right after constructor(), used to call asynchronous
tasks or network calls.
componentWillMount() {
console.log('componentWillMount called.’);
}
componentWillUpdate(nextProp, nextState) {
console.log('componentWillUpdate called.');
}
• 4. componentDidUpdate(): It is called after the rendering, this method
is mostly used to interact with updated rendering values and execute
any post-render events.
componentDidUpdate(prevProp, prevState) {
console.log('componentDidUpdate called.');
}
3. Unmounting method
• 1. componentWillUnmount(): It is called when the component is
removed from the DOM, Users can clear any running timers, stop
network requests and cleaning any previously stored value in the
application in this method.
• componentWillUnmount() {
• console.log('componentWillUnmount called.');
•}
Error handling method
• 1. The componentDidCatch(): It is a part of the error handling
method. It is used to find errors between JavaScript code and handle
them by passing the correct message or argument. It will help us to
use any try /cache block to handle any error.
• componentDidCatch(error, info) {
• console.log('componentDidCatch called.');
•}
References
• https://www.freecodecamp.org/news/functional-vs-class-component
s-react-native/#:~:text=Class%20components%20are%20used%20as,u
ser%20interactions%20or%20state%20updates
.
• https://www.javatpoint.com/react-native-state
• https://medium.com/wix-engineering/the-full-react-native-layout-che
at-sheet-a4147802405c
• https://aboutreact.com/react-native-application-life-cycle-of/