Facebook iconCreate, export and import components in React Native (vs JAVA)
Blogs/Technology

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

Nov 4, 20242 Min Read
Written by Murtuza Kutub
Create, Export, and Import Components in React Native (vs JAVA) Hero

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.

Partner with Us for Success

Experience seamless collaboration and exceptional results.

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.

Partner with Us for Success

Experience seamless collaboration and exceptional results.

Author-Murtuza Kutub
Murtuza Kutub

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.

Phone

Next for you

What is Flutter Widget Tree: A Comprehensive Guide Cover

Technology

May 8, 20258 min read

What is Flutter Widget Tree: A Comprehensive Guide

Flutter, Google’s open-source UI toolkit, has transformed the way developers build cross-platform applications. Its declarative approach to UI design, combined with a rich set of widgets, enables developers to create stunning, performant, and responsive applications.  At the core of Flutter’s architecture lies the widget tree, a fundamental concept that every Flutter developer must understand to build effective and efficient applications.  In this blog post, we’ll dive deep into the Flutter wi

Stateful vs Stateless: Choosing the Right Backend Architecture Cover

Technology

May 8, 20257 min read

Stateful vs Stateless: Choosing the Right Backend Architecture

When building scalable web platforms, whether it’s a crypto trading exchange, a real-time chess application, or a standard e-commerce store, one of the foundational architectural choices you must make is whether to design your backend as stateful or stateless. This decision has significant implications for scalability, performance, reliability, and user experience. Let’s explore the fundamental differences between these two approaches, examine practical use cases, and understand how technologie

What to Expect When Working with a Flutter Development Team? Cover

Technology

May 2, 20254 min read

What to Expect When Working with a Flutter Development Team?

Are you planning to make a mobile app and thinking about hiring a Flutter development team? Picking the right team can make or break your project, and knowing how the teamwork process works helps set clear expectations. Flutter has grown quickly, with over 46% of developers choosing it to build apps that work on multiple platforms. But working with a Flutter team is more than just sharing an idea and waiting for the final app. It’s a step-by-step process with planning meetings, regular updates