To create an animated progessbar we need to import theAnimated class. Add Animated.View and Animated.Text component inside View. Initialize a variable progress status to 0 and call the onAnimate() method. This method will invoked when the screen will completely loaded (componentDidMount() calls). Add a listener on onAnimate() method which updates the progress status.
App.js
import React, {Component} from ‘react’;
import {Platform, StyleSheet, Text, View, Animated} from ‘react-native’;
The progress status of progress bar is updated in the interval of per 0.5 second (50000 microseconds). At the same time the width of progress bar is determined by progress status, and its status is set into Animated.Text component.
The ProgressBarAndroid component of React Native is used to indicate the progress status of some activity or app loading something. ProgressBarAndroid component works only on Android platform. To use the progress bar in iOS platform, a ProgressViewIOS component is used.
Props of ProgressBarAndroid
Props
Type
Required
Description
animating
bool
no
It is used to show or hide the progress bar. Its default value is true to show, (false to hide).
color
color
no
It sets the color of progress bar.
indeterminate
indeterminateType
no
It shows the intermediate progress status of progress bar. It can only be false if the styleAtte of progress bar is Horizontal.
progress
number
no
It is a progress value between 0 and 1.
styleAttr
enum
no
It sets the style of progress bar. There are various style of progress bar in React Native such as Horizontal, Normal (default), Small, Large, Inverse, SmallInverse, and LargeInverse.
testID
string
no
It is used to locate this view in end-to-end tests.
React Native ProgressBarAndroid Example
In this example, we will implement horizontal ProgressBarAndroid and perform action on TouchableOpacity component. The progress status will display in Text component up to 3 digits only.
React Native WebView is a component which is used to load web content or web page. The WebView component is imports form core react-native library. Now, the WebView is replaced from the built-in core react-native, and placed in react-native-webview library.
Note: The React Native WebView recommended to import react-native-webview or react-native-community library.
Props of WebView
source
injectJavaScript
injectedJavaScript
onError
onLoad
onLoadEnd
onLoadStart
onMessage
originWhitelist
renderError
style
userAgent
html
url
geolocationEnabled
scrollEnabled
contentInset
bounces
allowFileAccess
nativeConfig
Types of WebView contents:
Display HTML code as a string: The HTML string code is passed into html prop inside source property.
<WebView
source={{html: ‘<h1>Hello javaTpoint</h1>’}}
/>
Display the internal web page: Create an internal web page inside a directory and pass its full path in source property.
<WebView
source={require(“./resources/index.html”)}
/>
Display the remote web page: A remote web page is loaded using uri tag with source property.
<WebView
source = {{ uri:’https://www.javatpoint.com’ }}
/>
React Native WebView Example 1
in this example, we will load the web page by passing its URL in source prop of WebView component.
Note: if you got an error message of module react-native-webview does not exist in the module map react native. Then make sure you install the below libraries:
React Native Switch is a Boolean control component which sets its value to true or false. It has an onValueChange callback method that updates its value prop. If the value prop is not changed, the Switch component continues supplied the value prop instead of the expected result of any user actions.
Props of Switch
Props
Description
disabled
It is a Boolean property, if it is set to true then it cannot be toggled to switch. Its default value is false.
trackColor
It is used to customize the color for switch track.
ios_backgroundColor
It set the custom background color in iOS. The background can be visible either when the switch value is disabled or when the switch is false.
onValueChange
It invoked when the switch value is changed.
testID
It is used to locate this view in end-to-end tests.
thumbColor
It colors the foreground switch grip. When it is set to iOS, the switch grip will lose its drop shadow.
tintColor
It sets the border color on iOS and background color on Android when the switch is turned off. This property is deprecated; use trackColor at the place of it.
value
It is the value of switch. When it is set to true, it turns on. The default value is false.
React Native Switch Example
In this example, we initialy set the Switch value to false and display a Text with ‘off’. When the value of Switch change to true by calling onValueChange the Text component is reset to ‘on’.
App.js
import React, { Component } from ‘react’
import {StyleSheet, Switch, View, Text} from ‘react-native’
React Native StatusBar is a component which is used to decorate status bar of the app. It is used by importing the StatusBar component from the react-native library. We can use multiple StatusBar at the same time.
<View>
<StatusBar
backgroundColor = “#b3e6ff”
barStyle = “dark-content”
/>
</View>
<View>
<StatusBar
backgroundColor = “#b3e6ff”
barStyle = “dark-content”
/>
<View>
<StatusBar
hidden={route.statusBarHidden} />
</View>
</View>
React Native StatusBar Props
Props
Description
animated
A status bar is animated if its property is changed. It supports backgrondColor, hidden, and barStyle.
barStyle
It sets the color of status bar text.
hidden
It is used to hide and show the status bar. By default, it is false. If hidden = {false} it is visible, if hidden = {true}, it hide the status bar.
backgroundColor
It sets the background color of the status bar.
translucent
When it is set of true, the app is built under the status bar.
showHideTransition
It displays the transition effect when showing and hiding the status bar. The default is ‘fade’.
networkActivityIndicatorVisible
It checks the network activity indicator is visible or not. It supports in iOS.
React Native StatusBar Methods
setHidden
setBarStyle
setBackgroundColor
setNetworkActivityIndicatorVisible
setTranslucent
React Native StatusBar Example 1
Let’s create a simple StatusBar example in which we change its background color.
React Native Picker is component which is used to select an item from the multiple choices. This is the same as a Dropdown option. Picker is used when we need to provide an alternative to choose from multiple options.
It is used by importing the Picker component from the react-native library.
Props of Picker
Prop
Description
onValueChange( itemValue, itemPosition)
It is a callback prop and called when an item is selected. It takes two parameters (itemValue: the item (value) which is selected, itemPosition: the position (index) of a selected item).
selectedValue
Returns the selected value.
style
It is a picket style type.
testID
It is used to locate this view in end-to-end tests.
enabled
It is a Boolean prop which makes picker disable when set to false. If it is set to false, the user cannot be able to make a selection.
mode
On Android, it specifies how to display the selections items when the users click on the picker. It has the “dialog” and “dropdown” properties. The dialog is default mode and shows as a modal dialog. The dropdown displays the picker as dropdown anchor view.
prompt
It is used in Android with dialog mode as the title of the dialog.
itemStyle
It styles each item of the picker labels.
React Native Picker Example
In this example, we create three label items in Picker component. When the item is selected from Picker, it calls the onValueChange callback and set the selected item in the picker. The index of item is read from itemPosition. The item’s position is displayed in a Text component.
App.js
import React, { Component } from ‘react’
import {StyleSheet,View, Text,Picker} from ‘react-native’
ActivityIndicator is used to display a circular loading indicator.
Props
Props
Description
animating
Option to show the indicator (bydefault it is true) or hide it (false).
size
Set the size of indicator (‘small’,’large’, number). The default size is small. Number format support only in android.
color
Set the foreground color of the spinner (default is gray).
hidesWhenStopped
It provides an option to show or hide the indicator when there is no animating (true by default).
React Native ActivityIndicator Example
In this example, the animating property set the activity indicator to true, and it is visible on the screen. When the component mounts, the animated activity indicator will be closed after six seconds using the closeActivityIndicator() method.
TextInput is the fundamental component to input text. It has several props which configure the different features, such as onChangeText that takes a function and call it whenever the text changed. The onSubmitEditing prop takes a function, which is called when the text submitted.
There are several things, which can be performed with text input, such as validating the text inside while user types.
React Native TextInput Example 1
In this example, we create a TextInput and perform an onChangeText action. At every text change, it calls the setState and checks the condition of a split. If the input text found ‘ ‘ space, it displays ‘?’ in the text. The text is placed in state as it is changed every time.
import React, { Component } from ‘react’;
import { AppRegistry, Text, TextInput, View } from ‘react-native’;
export default class PizzaTranslator extends Component {
{this.state.text.split(‘ ‘).map((word) => word && ‘?’).join(‘ ‘)}
</Text>*
</View>
);
}
}
Output
TextInput properties
allowFontScaling
autoCapitalize
autoCorrect
autoFocus
blurOnSubmit
caretHidden
clearButtonMode
clearTextOnFocus
contextMenuHidden
dataDetectorTypes
defaultValue
disableFullscreenUI
editable
enablesReturnKeyAutomatically
inlineImageLeft
inlineImagePadding
keyboardAppearance
keyboardType
maxLength
multiline
numberOfLines
onBlur
onChange
onChangeText
onContentSizeChange
onEndEditing
onFocus
onKeyPress
onLayout
onScroll
onSelectionChange
onSubmitEditing
placeholder
placeholderTextColor
returnKeyLabel
returnKeyType
scrollEnabled
secureTextEntry
selection
selectionColor
selectionColor
selectionState
selectTextOnFocus
spellCheck
textContentType
style
textBreakStrategy
underlineColorAndroid
The method .focus() and .blur() are exposed from the native element. These methods focus or blur the TextInput programmatically.
Multiline TextInput
The multiline props provide facility to input multiple lines of text. Some props of TextInput are only compatible with multiline, for example, multiline={true/false}. The property borderButtomColor will not work if multiline = false.
React Native TextInput Example 2
import React, { Component } from ‘react’;
import { AppRegistry, View, TextInput } from ‘react-native’;
class UselessTextInput extends Component {
render() {
return (
<TextInput
{…this.props} // Inherit any props passed to it; e.g., multiline, numberOfLines below
editable = {true}
maxLength = {40}
/>
);
}
}
export default class UselessTextInputMultiline extends Component {
Touchable components provide the capability to capture the tapping functionality. The touchables component can be implemented as an alternative of basic button, if they are not look right for your app. Using these components, you build your own button. Tapping on these components, you can display the feedback.
The touchables components do not provide any default styling, so you will need to do your style for presenting nicely in the app.
Types of Touchable Components
There are four touchable components provided by React Native. Selection of this component depends on the kind of feedback you want to provide:
TouchableHighlight
TouchableNativeFeedback
TouchableOpacity
TouchableWithoutFeedback.
React Native TouchableHighlight
The TouchableHighlight can be used where you would use a button or link on the web. The background of this component becomes dark on pressing it.
Props
Props
Type
Required
Platform
Description
activeOpacity
number
no
Determines the opacity of wrapped view when it is touched.
onHideUnderlay
function
no
Calls instantly after the underlay is hidden.
onShowUnderlay
function
no
Calls instantly after the underlay is shown.
style
View.style
no
underlayColor
color
no
Show the underlay color when the touch is active.
hasTVPreferredFocus
bool
no
iOS
It focuses TV preferred, work only for iOS.
tvParallaxProperties
object
no
iOS
It is an object with properties which control the Apple TV parallax effects.
React Native TouchableNativeFeedback
The TouchableNativeFeedback makes a view to response properly on touch. This component works only for Android operating system. It uses native state drawable to display the touch feedback.
It supports only a single View instance as a child node. It is implemented by replacing the View with another instance of RCTView node.
Props
Props
Type
Required
Description
background
backgroundPropType
no
It determines the background drawable that is going to be displayed as feedback.
useForeground
bool
no
It adds the ripple effect to the foreground of the view, instead of the background.
React Native TouchableOpacity
The TouchableOpacity wrapper is used to reduce the opacity of button. It allows background to be seen while the user press down. The opacity of button will be controlled by wrapping the children in an Animation.
Props
Props
Type
Required
Platform
Description
activeOpacity
number
no
It determines the opacity of wrapped view when it is touched.
tvParallaxProperties
object
no
iOS
It is an object with property which is used to control the Apple TV parallax effects.
hasTVPreferredFocus
bool
no
iOS
It focuses TV preferred, it works on Apple TV only.
Methods
Method
Description
setOpacityTo()
It animates the touchable to a new opacity.
React Native TouchableWithoutFeedback
The TouchableWithoutFeedback is used when the user wants to handle the tap functionality but doesn’t want to display any feedback.
Props
Props
Type
Required
Description
hitSlop
object
no
This defines how far your touch can start away from the button.
onAccessibilityTap
function
no
If accessible is set to true, the system invokes this function when the user performs accessibility tap gesture.
accessibilityHint
string
no
It helps user to understand what will happen when they perform an action on the accessibility element.
accessibilityLabel
node
no
It overrides the text, which is read by the screen reader when users interact with the element.
delayLongPress
number
no
It delays the onLongPress in milli-second calling onPressIn.
Some time user presses a view and holds it for the set of time. This long press is handled by the function using onLongPress props of any of the above “Touchable” components.
The React Native SectionList component is a list view component which sets the list of data into broken logical section. The broken data can be implemented using its section header prop renderSectionHeader.
To implement the SectionList component, we need to import SectionList from ‘react-native’ library.
Props of SectionList
sections
renderItem
initialNumToRender
keyExtractor
renderSectionHeader
renderSectionFooter
onRefresh
inverted
extraData
onEndReached
keyExtractor
legacyImplementation
onViewableItemsChanged
refreshing
removeClippedSubviews
ListHeaderComponent
SectionSeparatorComponent
stickySectionHeadersEnabled
onEndReachedThreshold
ListEmptyComponent
React Native SectionList Example
In this example, we create a SectionList with title and data. The sections prop is used to create the list of title and data values. The renderSectionHeader displays the header section of the list view.
import React, { Component } from ‘react’;
import { AppRegistry, SectionList, StyleSheet, Text, View } from ‘react-native’;
ItemSeparatorComponent prop adds the Separator between the lists of data. Using this prop, call a renderSeparatormethod in which we add a View component as separator.
renderSeparator = () => {
return (
<View
style={{
height: 1,
width: “100%”,
backgroundColor: “#000”,
}}
/>
);
};
ItemSeparatorComponent={this.renderSeparator}
Performing action on SectionList items
To perform an action on clicking (pressing) the list item, we use a onPress prop. The onPress prop and handle the event in another method getListViewItem.