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

Flutter Internationalization and Localization (Multilingual Support) Cover

Technology

Apr 22, 20253 min read

Flutter Internationalization and Localization (Multilingual Support)

Flutter apps aren't bound by geographical borders, so why should your audience be? Imagine reaching users across the globe by offering your app in their language. That’s exactly what Flutter's internationalization (i18n) and localization (l10n) make possible.  According to CSA Research, 76% of consumers prefer to purchase products presented in their native language, highlighting the significant value of localization in capturing global markets. Implementing Flutter internationalization and loc

Flutter Architecture Patterns: BLoC, Provider, Riverpod, and More Cover

Technology

Apr 22, 20253 min read

Flutter Architecture Patterns: BLoC, Provider, Riverpod, and More

Flutter, Google’s innovative UI toolkit, has exploded in popularity for building beautiful, cross-platform mobile apps. But as Flutter apps scale, choosing the right architecture pattern becomes crucial. Let's make it simple and dive into the most popular Flutter architecture patterns, including BLoC, Provider, Riverpod, and beyond. Whether you're building your first Flutter app or scaling a complex project, selecting the right architecture pattern can be the difference between a maintainable a

How To Test A Flutter App? A Beginner’s Guide Cover

Technology

Apr 22, 20253 min read

How To Test A Flutter App? A Beginner’s Guide

Building a Flutter app is exciting, but what if it breaks the moment users interact with it? Testing is often skipped by beginners, but it's your app's safety net in production. But what about testing?  Whether you're just starting or looking to level up your code quality, learning how to test a Flutter app is a game-changer. This guide breaks down the basics of Flutter app testing for beginners, because writing great code is only half the job. Making sure it works is the other half. Why Test