React Native – Hello World

Let’s create a very simple React Native app that displays a Hello World message and a button that changes the message when pressed. This example will cover basic components, state management, and styling.

Step-by-Step Guide

Step 1: Setting Up the Environment

First, ensure you have Node.js, Watchman, React Native CLI, and your preferred code editor installed.

Step 2: Creating a New React Native Project

Open your terminal and create a new React Native project:

npx react-native init SimpleApp
cd SimpleApp

Step 3: Writing the Basic App

Open App.js in your code editor and replace its content with the following code:

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

const App = () => {
  const [message, setMessage] = useState('Welcome to React Native!');

  const changeMessage = () => {
    setMessage('You pressed the button!');
  };

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.box}>
        <Text style={styles.text}>{message}</Text>
        <Button title="Press me" onPress={changeMessage} />
      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  box: {
    padding: 20,
    backgroundColor: 'lightblue',
    borderRadius: 10,
    alignItems: 'center',
  },
  text: {
    fontSize: 20,
    marginBottom: 20,
  },
});

export default App;

Explanation

  1. Imports:
    • React and useState from react for state management.
    • Basic components from react-native: SafeAreaView, View, Text, Button, StyleSheet.
  2. State Management:
    • useState is used to create a state variable message and a function setMessage to update it.
  3. changeMessage Function:
    • This function updates the message state when the button is pressed.
  4. Component Structure:
    • SafeAreaView: Ensures that the content is rendered within the safe area boundaries of a device.
    • View: A container for the text and button.
    • Text: Displays the current message.
    • Button: Triggers the changeMessage function when pressed.
  5. Styling:
    • StyleSheet.create: Creates a set of styles for the components.
    • Styles are applied using the style prop.

Step 4: Running the Application

Ensure you have a simulator or device set up. Run the app using the following command:

npx react-native run-android  # For Android
npx react-native run-ios      # For iOS

This simple app demonstrates the basics of React Native: setting up a project, using state to manage data, responding to user interactions, and applying styles. It’s a good starting point for beginners to understand how React Native works.


Tutorials Deck

TutorialsDeck is striving to provide the best learning material on technical and non-technical subjects.

Languages

Web Technologies

Database

Trending Technologies

© 2024. All rights reserved.

Contact Us @ tutorialsdeck06@gmail.com