No history yet

React Native Basics

React for Mobile Apps

If you know how to build web applications with React, you're already most of the way to building mobile apps. React Native is a framework that lets you use your existing React and JavaScript skills to create apps for both iOS and Android from a single codebase.

Key Mindset: You already know the logic of React (components, props, state). Your main task is learning the new building blocks for mobile.

Instead of building for a web browser, you're building for a mobile operating system. This means you won't use web elements like <div> or <span>. Instead, React Native provides a set of core components that correspond to the standard user interface elements on mobile platforms.

Core Building Blocks

React Native comes with a set of essential, ready-to-use components for building your UI. Think of them as the mobile equivalent of HTML tags. You'll use these for everything from displaying text to creating layouts.

Some of the most common components are:

  • <View>: The most fundamental component for building a UI. It's a container that supports layout with flexbox and styling.
  • <Text>: For displaying text.
  • <Image>: For displaying images.
  • <ScrollView>: A generic scrolling container.
  • <TextInput>: Allows the user to enter text.

You import these components directly from the react-native library and use them just like you would any other React component. The framework handles the work of turning them into the correct native UI elements for whatever device the app is running on.

State and Props

Managing data in React Native works exactly like it does in React for the web. Components are still the fundamental building blocks, and they manage their data using props and state.

props (short for properties) let you pass data from a parent component to a child component. They are read-only.

state is used for data that is internal to a component and is expected to change over time. When a component's state changes, it re-renders.

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

const Counter = () => {
  // Using the useState Hook to manage state
  const [count, setCount] = useState(0);

  return (
    <View>
      <Text>You clicked {count} times</Text>
      <Button
        onPress={() => setCount(count + 1)}
        title="Click me!"
      />
    </View>
  );
};

export default Counter;

This simple Counter component uses the useState Hook to keep track of a number. Every time the button is pressed, the count state is updated, and the component re-renders to display the new value. If you've used React, this should feel very familiar.

How It All Works

So how does JavaScript code become a native mobile app? React Native uses a concept often called "the bridge." Your JavaScript code runs in its own thread, separate from the main UI thread of the native platform (iOS or Android).

Lesson image

When your app starts, the native code loads your JavaScript bundle. When you render a component like <View>, the JavaScript thread sends a serialized message across the bridge to the native thread. The message essentially says, "Hey, create a native view here."

The native side receives this message and creates the corresponding native UI element (a UIView on iOS or a View on Android). This separation allows your app's UI to remain smooth and responsive, even if your JavaScript code is busy with complex calculations.

The bridge is asynchronous, so communication between the JavaScript and native threads doesn't block either one. This is key to maintaining high performance in a React Native app.

Understanding this architecture is the first step. You write your logic in JavaScript using React's familiar component model, and React Native handles the communication to render a truly native user interface.

Quiz Questions 1/5

What is the primary advantage of using React Native for mobile app development?

Quiz Questions 2/5

In React Native, you use components like <View> instead of web elements like <div>. Where do you import these core components from?