Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is React Native?

React Native is a framework for building mobile applications using JavaScript and it is built by Facebook. But before diving into the details of React Native, let's talk about what mobile applications are and why we would want to use this particular framework.

What are Mobile Applications?

Mobile applications, or "apps" for short, are software applications designed to run on mobile devices, such as smartphones and tablets. They come in various forms, including games, social media platforms, and productivity tools. You probably use some of them on a daily basis, like WhatsApp, Instagram, or Facebook.

Traditionally, mobile applications were written in platform-specific programming languages, such as Java for Android and Swift/Objective-C for iOS. This meant that developers had to write separate codebases for each platform, making the development process time-consuming and expensive.

Introducing React Native

React Native solves this problem by allowing developers to write their applications in JavaScript, which is then translated into native code for each platform. This means that you can write your app once and run it on both Android and iOS devices, saving you a lot of time and resources. Additionally, since JavaScript is a widely-known and popular language, it's easier to find developers who can work with React Native.

Now that we know what React Native is and why we want to use it, let's explore its main components and how they work together to create a mobile application.

Components and Props

React Native is based on React, a popular JavaScript library for building user interfaces. In React, we create components, which are like building blocks that make up our application. A component can be as simple as a button, or as complex as an entire screen.

Let's create a simple "Hello, World!" application using React Native. In a new file called App.js, write the following code:

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

const App = () => {
  return (
    <Text>Hello, World!</Text>
  );
};

export default App;

This code defines a simple component called App, which renders a Text component with the content "Hello, World!". We can think of the Text component as a building block provided by React Native that represents a piece of text in our application.

Components can also have properties, or "props". Props are like inputs that we can pass to a component to customize its appearance or behavior. For example, let's say we want to create a button component that we can reuse throughout our application. We might want to pass a prop to the button, like a label or an action to perform when the button is pressed.

In a new file called Button.js, write the following code:

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

const Button = ({ label, onPress }) => {
  return (
    <TouchableOpacity onPress={onPress}>
      <Text>{label}</Text>
    </TouchableOpacity>
  );
};

export default Button;

Now we have a reusable Button component that accepts two props: a label for the button's text, and an onPress function that will be called when the button is pressed. We can use this button in our App component like this:

import React from 'react';
import { View } from 'react-native';
import Button from './Button';

const App = () => {
  const handleButtonPress = () => {
    alert('Button pressed!');
  };

  return (
    <View>
      <Button label="Press me!" onPress={handleButtonPress} />
    </View>
  );
};

export default App;

Now our app has a button that displays an alert when pressed!

State and Lifecycle

In addition to props, components can also have state. State is an object that holds information about the component and determines how it should render. When the state changes, the component will re-render, updating its appearance.

Let's say we want to create a counter app that increments a number every time a button is pressed. We need to store the count as state in our component.

Update the App.js file to look like this:

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

const App = () => {
  const [count, setCount] = useState(0);

  const handleButtonPress = () => {
    setCount(count + 1);
  };

  return (
    <View>
      <Text>Count: {count}</Text>
      <Button label="Increment" onPress={handleButtonPress} />
    </View>
  );
};

export default App;

Here, we use the useState hook provided by React to create a state variable called count, initialized to 0. The setCount function is used to update the state. When the button is pressed, the handleButtonPress function updates the count and the component re-renders, showing the updated value.

Styles and Layout

React Native uses a subset of CSS to style components. Styles are written as JavaScript objects, and React Native provides a StyleSheet API to create and manage them more efficiently.

To apply styles to our counter app, update the App.js file like this:

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

const App = () => {
  const [count, setCount] = useState(0);

  const handleButtonPress = () => {
    setCount(count + 1);
  };

  return (
    <View style={styles.container}>
      <Text style={styles.count}>Count: {count}</Text>
      <Button label="Increment" onPress={handleButtonPress} />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  count: {
    fontSize: 24,
  },
});

export default App;

Now our app has some basic styling! We've centered the content on the screen and increased the font size of the count.

Putting It All Together

We've learned about the main building blocks of a React Native application, including components, props, state, and styling. By combining these concepts, we can create complex mobile applications that run on both Android and iOS.

Remember that React Native is just one way to build mobile applications. There are other frameworks and tools out there, but React Native stands out due to its popularity, ease of use, and ability to create high-performance apps with a single codebase.

As a beginner learning programming, React Native can be an excellent choice for building mobile applications. With JavaScript at its core, you can leverage your existing knowledge while learning new concepts specific to mobile development. So go ahead and give React Native a try, and happy coding!