Prahlad Inala
Prahlad Inala

Follow

Prahlad Inala

Follow
React Native 101: Rewriting an entire App.jsx

React Native 101: Rewriting an entire App.jsx

Prahlad Inala's photo
Prahlad Inala
ยทApr 21, 2023ยท

3 min read

Play this article

Table of contents

Introduction

Importance of rewriting an entire App.jsx

Rewriting an entire App.jsx file in a React Native project can be challenging, But we can learn a lot from it.

A brief overview of the steps involved

  1. Analyzing the existing code in App.jsx .

  2. Remove the existing code from App.jsx.

  3. Start writing code in App.jsx from scratch.

Understanding the existing App.jsx

Analyzing the code structure

// IMPORT STATEMENTS

// MAIN (APP) FUNCTION -> APP COMPONENT

// EXPORT APP COMPONENT / MAIN (APP) FUNCTION
import React from 'react';
import type {PropsWithChildren} from 'react';
import {
  SafeAreaView,
  ScrollView,
  StatusBar,
  StyleSheet,
  Text,
  useColorScheme,
  View,
} from 'react-native';

import {
  Colors,
  DebugInstructions,
  Header,
  LearnMoreLinks,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

type SectionProps = PropsWithChildren<{
  title: string;
}>;

function Section({children, title}: SectionProps): JSX.Element {
  const isDarkMode = useColorScheme() === 'dark';
  return (
    <View style={styles.sectionContainer}>
      <Text
        style={[
          styles.sectionTitle,
          {
            color: isDarkMode ? Colors.white : Colors.black,
          },
        ]}>
        {title}
      </Text>
      <Text
        style={[
          styles.sectionDescription,
          {
            color: isDarkMode ? Colors.light : Colors.dark,
          },
        ]}>
        {children}
      </Text>
    </View>
  );
}

function App(): JSX.Element {
  const isDarkMode = useColorScheme() === 'dark';

  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  return (
    <SafeAreaView style={backgroundStyle}>
      <StatusBar
        barStyle={isDarkMode ? 'light-content' : 'dark-content'}
        backgroundColor={backgroundStyle.backgroundColor}
      />
      <ScrollView
        contentInsetAdjustmentBehavior="automatic"
        style={backgroundStyle}>
        <Header />
        <View
          style={{
            backgroundColor: isDarkMode ? Colors.black : Colors.white,
          }}>
          <Section title="Step One">
            Edit <Text style={styles.highlight}>App.tsx</Text> to change this
            screen and then come back to see your edits.
          </Section>
          <Section title="See Your Changes">
            <ReloadInstructions />
          </Section>
          <Section title="Debug">
            <DebugInstructions />
          </Section>
          <Section title="Learn More">
            Read the docs to discover what to do next:
          </Section>
          <LearnMoreLinks />
        </View>
      </ScrollView>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
  },
  highlight: {
    fontWeight: '700',
  },
});

export default App;

After removing the existing code

Error due to our app is unable to find the App Component, which index.js is expecting

index.js

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);

Designing the new App.jsx

Defining the App component

import React from 'react';
import {View, Text, SafeAreaView} from 'react-native';

function App() {
  return (
    <SafeAreaView>
      <View>
        <Text>Hello World !</Text>
        <Text>Hello World !</Text>
        <Text>Hello World !</Text>
        <Text>Hello World !</Text>
      </View>
    </SafeAreaView>
  );
}

export default App;

Building the new App.jsx

Run the app: To run the app on a physical device or emulator, navigate to your project directory and run the following commands:

For Android:

npx react-native run-android

For iOS:

npx react-native run-ios

Now we can see the app launching on our Emulator or a Physical device

Did you find this article valuable?

Support Prahlad Inala by becoming a sponsor. Any amount is appreciated!

See recent sponsors |ย Learn more about Hashnode Sponsors
ย 
Share this