5 min to read
React Native is a JavaScript framework for writing real, natively rendering mobile applications for iOS and Android. It is based on React, Facebook’s JavaScript library for building user interfaces and it targets mobile platforms.
React Native applications are written using a mixture of JavaScript and XML-Esque markup, known as JSX. Then, under the hood, the React Native “bridge” invokes the native rendering APIs in Java (for Android) and Objective-C (for iOS). Thus, your application will be rendered using real mobile UI components, not reviews, and it will look and feel like any other mobile application. React Native also exposes JavaScript interfaces for platform APIs, so your React Native apps can access platform features like the phone camera, or the user’s location.
It is a cross-platform Tab View component for React Native. It is implemented using react–native–pager-view on Android & iOS.
Before you start you should have WebStorm or VSCode, expo and Node.js installed on your computer.
Open your terminal and follow along
npm install -g expo-cli
This will install expo-CLI in your system globally.
Open the command prompt in the location where you want to create your project.
In the command prompt write the following commands:
expo init newapp
cd newapp
You will find a folder named ‘new app’ in your directory where you created the project. Open that in WebStorm/ VSCode.
Open a Terminal in the project root and run:
yarn add react-native-tab-view
Now we need to install react-native-pager-view.
yarn add react-native-pager-view
Now we have to run the app using the following command:
expo start
To run the app on your device:
Install expo from play store.
Scan the QR code from the Expo Server started in your browser.
This will start the application on your device
The package – react-native-tab-view exports a TabView component which is the one you’d use to render the tab view, and a TabBar component which is the default tab bar implementation used in this package.
Let's try to understand the components at an individual level:
It is a container component responsible for managing and rendering tabs. It follows material design styles by default.
Its usage is as follows: Basic usage looks like this:
<TabView
navigationState={{ index, routes }}
renderScene={renderScene}
renderTabBar={renderTabBar}
onIndexChange={setIndex}
initialLayout={{ width: layout.width }}
/>
It represents the state of the tab view. The state should contain the following properties:
Each route object should contain the following properties:
Example:
const [index, setIndex] = React.useState(0);
const [routes] = React.useState([
{ key: 'first', title: 'First' },
{ key: 'second', title: 'Second' },
]);
TabView is a controlled component, which means the index needs to be updated via the onIndexChange callback.
It is a callback that is called on tab change, it receives the index of the new tab as an argument. The navigation state needs to be updated when it’s called, otherwise, the change is dropped.
It is a callback that returns a react element to render as the page for the tab. It receives an object containing the route as the argument.
You need to make sure that your individual routes implement a shouldComponentUpdate to improve the performance. To make it easier to specify the components, you can use the SceneMap helper.
SceneMap takes an object with the mapping of route.key to React components and returns a function to use with renderScene prop.
const FirstRoute = () => (
<View style={{ flex: 1, backgroundColor: 'grey'}}>
<Text>Tab One</Text>
</View>
);
const SecondRoute = () => (
<View style={{ flex: 1, backgroundColor: 'darkgrey'}} >
<Text>Tab Two</Text>
</View>
);
const renderScene = SceneMap({
first: FirstRoute,
second: SecondRoute,
});
It is a callback that returns a custom React Element to use as the tab bar:
const renderTabBar = props => (
<TabBar
{...props}
activeColor={'white'}
inactiveColor={'black'}
style={{marginTop:25,backgroundColor:'red'}}
/>
);
If this is not specified, the default tab bar is rendered. You pass these props to customize the default tab bar, provide your own tab bar, or disable the tab bar completely.
It is the object containing the initial height and width of the screens. This will improve the initial rendering performance. For most apps, this is a good default:
<TabView
initialLayout={{ width: layout.width }}
/>
There are various other properties that can be implemented in TabView for various other purposes such as styling the component etc.
The entire code can be summed up as:
A view that switches between multiple child views using interactive user interface elements.
React components are a great way of using encapsulation. Just create a component and use it wherever needed. React component-based structure has proven its power not only for dynamic sites but also for content-based sites.
Tabs organize content across different screens, data sets, and other interactions. TabView enables swipeable tabs.
Tags
Are you looking for something specific?
We understand that hiring is a complex process, let’s get on a quick call.
Share
11 comments