Creating UIs Using React Native Components

Tutorial 2 of 5

1. Introduction

Goal of the Tutorial

This tutorial aims to guide you on how to create user interfaces (UIs) using React Native components. By the end of this tutorial, you will be able to build interactive UIs for your mobile applications using the power of React Native.

Learning Objectives

  • Understand what React Native components are.
  • Learn how to use different React Native components.
  • Learn how to create a simple yet interactive UI using React Native components.

Prerequisites

  • Basic understanding of JavaScript.
  • Familiarity with ReactJS would be beneficial but not necessary.
  • Have Node.js and npm installed on your machine.
  • Have React Native CLI installed on your machine.

2. Step-by-Step Guide

Understanding React Native Components

React Native components are the building blocks of your application's UI. They are JavaScript functions that return a React element. These elements describe what should be rendered on the screen.

Using React Native Components

To use a React Native component, import it from the 'react-native' module and use it as a normal JSX tag. For example, the Text component is used to render text:

import { Text } from 'react-native';

<Text>Hello, World!</Text>

3. Code Examples

Example 1: Using the View and Text Components

The View component is like the div of React Native. It is a container that supports layout with Flexbox, style, touch handling, and accessibility controls. The Text component, as discussed, is used to display text.

Here is an example of how to use these two components:

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

export default function App() {
  return (
    <View style={{padding: 50}}>
      <Text>Hello, World!</Text>
    </View>
  );
}

In this code snippet:
- We import the necessary modules and components.
- We define a function component called App.
- The App function returns a View component with a Text child component.
- The View component has a padding of 50px, and the Text component displays the string "Hello, World!".
- The expected output is a screen with the text "Hello, World!" and a padding of 50px around the text.

4. Summary

In this tutorial, we've covered the basics of React Native components and how to use them to build UIs. We've seen how to use the View and Text components for basic layout and text display, respectively.

Next, you should explore other components available in React Native, such as Image, Button, and ScrollView. You can find the documentation for these components and more in the React Native documentation.

5. Practice Exercises

Exercise 1

Create a screen with a Button component. When the button is pressed, display a Text component that says "Button Pressed!".

Exercise 2

Create a screen with a ScrollView component. Inside the ScrollView, render 20 Text components, each displaying a different number from 1 to 20.

Exercise 3

Create a screen with an Image component displaying any image of your choice from the web.

Solutions

Here are solutions to the exercise problems:

Solution 1

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

export default function App() {
  const [buttonPressed, setButtonPressed] = useState(false);

  return (
    <View style={{padding: 50}}>
      <Button title="Press me" onPress={() => setButtonPressed(true)} />
      {buttonPressed && <Text>Button Pressed!</Text>}
    </View>
  );
}

Solution 2

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

export default function App() {
  return (
    <ScrollView>
      {[...Array(20).keys()].map((i) => (
        <Text key={i}>{i + 1}</Text>
      ))}
    </ScrollView>
  );
}

Solution 3

import React from 'react';
import { Image } from 'react-native';

export default function App() {
  return (
    <Image
      source={{uri: 'https://example.com/my-image.jpg'}}
      style={{width: 200, height: 200}}
    />
  );
}

Remember, the key to mastering React Native components is practice. Keep building different screens and trying out different components. Happy coding!