What is a Component?
The Components are the building blocks in React Native which is similar to the container where all UI elements do accumulate and help to render details in the foreground as a native UI on either Android or iOS devices.
I assume that React Native is installed on your machine, If not no worries, Please refer my previous blog post on Environment setup.
import React from 'react';
import { Text, AppRegistry } from 'react-native';
const App = () => (
<Text>Hello Component!</Text>
);
AppRegistry.registerComponent('ComponentDemo', () => App);
Copy-paste the above code snippets into index.js. You’re right to go. Yet confused? Chill! Let’s break the code into pieces in a better way.
React Native JAVA
import React from 'react'; import java.util.*;
import { Text, AppRegistry }
from 'react-native'; import java.text.*;
const App = () => ( public class App {
); }
AppRegistry.registerComponent package com.componentdemo
('ComponentDemo', () => App);
const — This is similar to a class in JAVA, which lets you declare a component block in React Native.
AppRegistry — is the kick starter helps to build React Native apps where all parent or root component should be registered using ‘registerComponent’ function.
Text — This is similar to TextView in Android and Label in iOS.
It’s simple. Create ‘src/components/YOUR_FILE_NAME.js’ inside your project root folder.
Create a component ‘Title’ and export it as a reusable component.
For Example:
export default Title; //Provides export access for the component
Code Implementation:
import React from 'react';
import { Text } from 'react-native';
const Title = () => (
<Text>Hello Title</Text>
);
export default Title;
Add the following import statement in the destination Component .js file.
import Title from './src/components/importcomponentdemo';
Complete code:
import React from 'react';
import { AppRegistry } from 'react-native';
import App from './src/components/importcomponentdemo';
const App = () => (
<Title />
);
AppRegistry.registerComponent('ComponentDemo', () => App);
Output:
“Hello Title”
Yes! You’re done. Now it’s possible to access <Title> property from the base file.
A product development and growth expert, helping founders and startups build and grow their products at lightning speed with a track record of success. Apart from work, I love to network & Travel.
Improve Your Shopify Store with a "Sticky Add to Cart" Feature
In this ever-changing fast-paced world of e-commerce where the increase of competitors gives buyers an advantage of choice over different brands, it becomes seamlessly important to grab and retain their attention towards what...
Crafting Seamless Email Experiences: CSS Tips for Bulletproof Email Templates
Have you ever struggled with an email template only to see it fall apart in various inboxes? We understand, which is why CSS steps in to save the day as your email superhero. I recently created several email templates while...
A HR Odyssey in Goal Setting
Setting goals! It can turn out to be a total game-changer! But here's what goals are. Your vision of the future. Now, there are two ways to face the future. One, with apprehension. Number two, with anticipation. Unfo...