{ "@context": "https://schema.org", "@type": "BlogPosting", "mainEntityOfPage": { "@type": "WebPage", "@id": "https://www.f22labs.com/blogs/create-export-and-import-components-in-react-native-vs-java/" }, "headline": "Create, Export, and Import Components in React Native (vs JAVA)", "image": [ "https://www.f22labs.com/_next/image/?url=https%3A%2F%2Fghost.f22labs.cloud%2Fcontent%2Fimages%2F2023%2F09%2Fcomponentsinreactnative-1.webp&w=1920&q=75" ], "datePublished": "2017-11-01", "dateModified": "2024-01-08", "author": { "@type": "Person", "name": "Murtuza Kutub" }, "publisher": { "@type": "Organization", "name": "F22 Labs", "logo": { "@type": "ImageObject", "url": "https://www.f22labs.com/_next/static/media/logo.cab716e3.svg" } }, "description": "Crafting, exporting, and importing components in React Native vs. Java made easy. A guide to streamline your development process.", "keywords": "React Native vs JAVA", "url": "https://www.f22labs.com/blogs/create-export-and-import-components-in-react-native-vs-java/", "wordCount": "726", "speakable": "https://www.f22labs.com/blogs/create-export-and-import-components-in-react-native-vs-java/" }

Create, Export, and Import Components in React Native (vs JAVA)

React native vs Java

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.

How to create a Component?

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.

How to export a Component ?

It’s simple. Create ‘src/components/YOUR_FILE_NAME.js’ inside your project root folder.

Component demo

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;

How to import a Component from different .js file?

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.