EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

React Native Components

By Rahul DubeyRahul Dubey

Home » Software Development » Software Development Tutorials » React Native Tutorial » React Native Components

React Native Components

Introduction to React Native Components

React Native provides various built-in components. Through components, one can break the UI into reusable, independent pieces, and can think about each piece in isolation. The components in React Native are defined as functions or classes. To define the React component class, a React.component is extended. To define a React.component subclass, the only method is known as render(). It has several “lifecycle methods” that can be overridden to run code at a particular time in the whole process. They are similar to JavaScript Functions.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

class  Welcome  extends  React.Component  { render() {
return  <h1>  Hello,  {this.props.name}</h1>;
}
}

Components & Uses of React Native

Below are the components of react native with its uses:

Example #1 – View

Use: To build a user interface, the view is the most important component.

Code:

import React, { Component } from 'react';
import {AppRegistry,StyleSheet,Text,View} from 'react-native';
export default class NativeSample extends Component { render() {
return (
<View style={{backgroundColor:"yellow",margin:5}}>
<Text>Parent View</Text>
<View style={{backgroundColor:"red",margin:5}}>
<Text>first Child view</Text>
<View style={{backgroundColor:"green",margin:5}}>
<Text>Second Child view</Text>
</View>
</View>
</View>
);
}
}
AppRegistry.registerComponent('NativeSample', () => NativeSample);

Output:

React Native Components Example 1

Example #2 – StyleSheet

 Use: It can be defined as the style in which the mobile application will appear, so it is used to style the mobile application and will make the application attractive.

Popular Course in this category
React JS Redux Training (1 Course, 5 Projects)1 Online Courses | 5 Hands-on Projects | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,487 ratings)
Course Price

View Course

Related Courses

 Code:

import React, { Component } from 'react';
import {AppRegistry,StyleSheet,Text,View} from 'react-native';
export default class NativeSample extends Component {
constructor() {
super()
this.state = {
firstVar: 'It displays using styleSheet'
}
}
render() {
return (
<View >
<Text style={styles.textStyle} > {this.state.firstVar}</Text>
</View>
);
}
}
AppRegistry.registerComponent('NativeSample', () => NativeSample);
const styles = StyleSheet.create ({
textStyle: {
margin: 50,
fontSize: 30,
color:'red',
fontWeight: 'bold',
}
})

Output:

React Native Components Example 2

Example #3 – TextInput

Use: It is a component which is used to input text into the mobile application through a keyboard.

Code:

import React, { Component } from 'react';
import {AppRegistry,StyleSheet,Text,TextInput,View} from 'react-native';
export default class NativeSample extends Component {
constructor() {
super()
this.state = {
firstVar: ' '
}
}
render() {
return (
<View style={{margin:50}}>
<TextInput style={{height: 40}}
placeholder="Type your text!"
onChangeText={(firstVar) => this.setState({firstVar})}
/>
<Text style={{padding: 10, fontSize: 42}}>
{this.state.firstVar}
</Text>
</View>
);
}
}
AppRegistry.registerComponent('NativeSample', () => NativeSample);

Output:

 React Native Components Example 3

Example #4 – ScrollView

Use: It is used to provide a large list or large content in view with the scrollbar. It helps in viewing the large content.

Code:

import React, { Component } from 'react';
import {AppRegistry,StyleSheet,Text,ScrollView,View} from 'react-native';
export default class NativeSample extends Component {
constructor() {
super()
this.state = {
firstVar: ' ',
listvalue: [
{'name':'list1',               'id':         1},
{'name':'list2',               'id':         2},
{'name':'list3',               'id':         3},
{'name':'list4',               'id':         4},
{'name':'list5',               'id':         5},
{'name':'list6',               'id':         6},
{'name':'list7',               'id':         7},
{'name':'list8',               'id':         8},
{'name':'list9',               'id':         9},
{'name':'list10',             'id':         10},
{'name':'list11',             'id':         11},
{'name':'list12',             'id':         12},
] }
}
makeList= (item) => (
<Text
key={item.id}
style={styles.list}>
{item.name}
</Text>
);
render() {
return (
<View style={{margin: 50,
height: 500,}}>
<Text>Scroll View</Text>
<ScrollView>
{ this.state.listvalue.map(this.makeList)}
</ScrollView>
</View>
);
}
}
AppRegistry.registerComponent('NativeSample', () => NativeSample);
const styles = StyleSheet.create ({
list: {
margin: 15,
padding: 5,
height: 40, borderColor: 'red', borderWidth: 1
}
})

Output:

React Native Components Example 4

Example #5 – ListView

Use: It provides the dataSource array value to the view. ListView is used in displaying the vertically scrolling list of changing data.

Code:

import React, { Component } from 'react';
import {AppRegistry,StyleSheet,Text,ListView,View} from 'react-native';
export default class NativeSample extends Component {
constructor() {
const ds = new ListView.DataSource({rowHasChanged: (r2, r3) => r2 !== r3});
super()
this.state = {
dataSource: ds.cloneWithRows(['row 1', 'row 2', 'row 3', 'row 4', 'row 5','row6','row7','row8']),
}
}
render() {
return (
<View style={{margin: 50,
height: 500,}}>
<Text>List View</Text>
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData}</Text>}
/>
</View>
);
}
}
AppRegistry.registerComponent('NativeSample', () => NativeSample);

Output:

 Example 5 (ListView)

Example #6 – Container Component

Use: It is used to handle all functionalities and states.

 Code:

import React, { Component } from 'react';
import { AppRegistry, View,Text } from 'react-native';
import presentationalComponent from './presentationalComponent';
class Helloworld extends Component {
constructor() {
super()
this.state = {
firstVar: 'It display using state'
}
}
hideText = () => {
this.setState({firstVar: ' '})
}
render() {
return (
<View style={{backgroundColor:'blue'}}>
<presentationalComponent
myText = {this.state.firstVar}
deleteText = {this.hideText}
/>
</View>
);
}
}
AppRegistry.registerComponent('Helloworld', () => Helloworld);

Example #7 – Presentation Component

Use: It is used to display the view by using the props.

Code:

import React, { Component } from 'react'
import {Text,View} from 'react-native'
const presentationalComponent = (props) => {
return (
<View>
<Text onPress = {props.deleteText}>
{props.myText}
</Text>
</View>
)
}
export default presentationalComponent

Example #8 – FlexDirection

Use: It is used to define the primary axis of the layout.

Code:

import React, { Component } from 'react';
import {AppRegistry,StyleSheet,Text,View} from 'react-native';
export default class NativeSample extends Component {
render() {
return (
<View style={{flex: 1, flexDirection: 'row'}}>
<View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'red'}} />
<View style={{width: 50, height: 50, backgroundColor: 'green'}} />
</View>
);
}
}
AppRegistry.registerComponent('NativeSample', () => NativeSample);

Output:

 Example 8 (FlexDirection)

Example #9 – AlignItems

Use: It is used to determine the alignment in the secondary axis of the children.

Code:

import React, { Component } from 'react';
import {AppRegistry,StyleSheet,Text,View} from 'react-native';
export default class NativeSample extends Component {
render() {
return (
<View style={{flex: 1, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center'}}>
<View style={{width: 50, height: 50, backgroundColor:'blue'}} />
<View style={{width: 50, height: 50, backgroundColor:'red'}} />
<View style={{width: 50, height: 50, backgroundColor:'green'}} />
</View>
);
}
}
AppRegistry.registerComponent('NativeSample', () => NativeSample);

Output: 

Example 9 (AlignItems)

Conclusion

On the basis of the above discussion, it is used to develop the User Interface of the mobile application. We also got to know about the uses of different components of React Native. As there is a huge variety of built-in components in the React Native which helps in making the mobile applications for both iOS and Android with great User Interface. So we can say that a component is a JavaScript function that optionally accepts input and returns React element.

Recommended Articles

This is a guide to React Native Components. Here we discuss the introduction to React Native Components and its Examples along with Code Implementation. You can also go through our other suggested articles to learn more –

  1. What is React Native?
  2. React Native Layout
  3. React Native FlatList
  4. React Native AsyncStorage

React JS Redux Training (1 Course, 4 Projects)

1 Online Courses

5 Hands-on Projects

18+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
React Native Tutorial
  • Basic
    • What is React Native?
    • React Versions
    • React Constructor
    • React Native Architecture
    • React Native Libraries
    • React Components Libraries
    • React Native Components
    • React Component Library
    • React Component Lifecycle
    • React Native Framework
    • React Higher Order Component
    • React Ternary Operator
    • React Native Charts
    • React Native Layout
    • React Native Grid
    • React Native Fetch
    • React Native Modal
    • React Native SVG
    • Button in React Native
    • React List Components
    • React Native Element
    • React Native FlatList
    • React Native SectionList
    • react native dropdown
    • React Native Menu
    • React Native State
    • React State Management
    • React Native Tabs
    • React Native Tab Bar
    • React Format
    • React-Native Switch
    • React Native Firebase
    • React Native Flexbox
    • React Native StatusBar
    • React Native ScrollView
    • React Native ListView
    • React Native TextInput
    • React Native Table
    • React-Native Border Style
    • React Native Search Bar
    • React-Native StyleSheet
    • React Native Vector Icons
    • React Native Login Screen
    • React Native Splash Screen
    • React Native Image Picker
    • React Native Navigation
    • React Native Swift
    • React Controlled Input
    • React Fragment
    • React Native Color
    • React Portals
    • React Refs
    • React shouldComponentUpdate()
    • React ComponentDidMount()
    • React componentWillUpdate()
    • React Native Orientation
    • React Native Animation
    • React Native Form
    • React Props
    • React Native Linear Gradient
    • React Native AsyncStorage
    • React Error Boundaries
    • React Native Progress Bar
    • React-Native Calendar
    • React Native Linking
    • React Native DatePicker
    • React Native Image
    • React Native Drawer
    • React Native Drawer Navigation
    • React Native Fonts
    • React Native Overlay
    • React Native OneSignal
    • React Native Template
    • React Native Router
    • React Router Transition
    • React Dispatcher
    • React Native Redux
    • React JSX
    • React native keyboardavoidingview
    • React Native Permissions
    • React Redux Connect
    • React Native Material
    • React Native Gesture Handler
    • React Native Theme
    • React Native Accessibility
    • React Native Justify Content
    • MobX React Native
    • React Native Authentication
    • React Native UUID
    • React Native Geolocation
    • React Native Debugger
    • React Native Carousel
    • React Native Local Storage
    • React Native TypeScript
    • React Bootstrap
    • React Native SQLite
    • React Native Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - React JS Redux Training (1 Course, 4 Projects) Learn More