Skip to content
REACT Instructional Materials Index Page
Share
Explore

REACT Instructional Materials Index Page

REACT



Core Concepts of REACT


In React, UI elements are divided into reusable components, which are independent and self-contained pieces of code that can be easily rendered and reused throughout the application. This allows developers to build complex user interfaces in a modular and scalable way, without having to worry about the underlying implementation details.
Another important feature of React is its virtual DOM, which is a lightweight representation of the actual DOM. This allows React to update the UI efficiently by comparing the virtual DOM with the actual DOM and making the minimum necessary changes. This results in faster and more efficient UI updates, which can improve the overall performance and user experience of the application.
In addition, React's use of JavaScript and JSX syntax allows developers to write code in a declarative and easy-to-read manner, which can lead to faster development times and easier maintenance. Finally, React has a large and active community of developers, which provides access to a wealth of resources, documentation, and third-party libraries that can further enhance the development process.

How does React Native enable you to build mobile apps that run on both Android and iOS platforms using a single codebase.

React Native allows you to build mobile apps that run on both Android and iOS platforms using a single codebase by utilizing a "write once, run anywhere" approach. Essentially, this means that you can write code in JavaScript and use it to create a mobile app that will work seamlessly on both Android and iOS devices.
Here's how React Native achieves this:
Native Components: React Native uses native components that are available on both platforms. For example, instead of using HTML elements as in web development, it uses native UI elements like buttons, text fields, and images.
Cross-Platform APIs: React Native also includes cross-platform APIs that enable you to access features like camera, location, and storage on both platforms using a single codebase.
JavaScript Runtime: React Native runs JavaScript code in a separate thread using a JavaScript runtime, which communicates with the native components and APIs of the platform. This allows the app to run smoothly and provide a seamless user experience.
Platform-Specific Code: While React Native allows you to write code once and use it across both platforms, there may be some platform-specific code that needs to be added. In such cases, you can write platform-specific code in Java or Kotlin for Android and Objective-C or Swift for iOS and integrate it into your React Native codebase.
Overall, React Native's approach enables you to write code once and use it across both platforms, which can save you time and effort while still providing a high-quality, native mobile app experience for your users.

JSX

In React Native, JSX (JavaScript XML) is used to write views and handle UI (User Interface) components. JSX is a syntax extension for JavaScript that allows developers to write HTML-like code within their JavaScript code, which makes it easier to create and manage UI components in a more readable and intuitive way.
React Native is a framework developed by Facebook that enables developers to build mobile applications for iOS and Android using a single codebase written in JavaScript and React. React Native uses native components instead of web components, which results in a more performant and native-like experience for the end user.
When using React Native, you define components using JSX syntax, which are then compiled into native UI elements for the respective platforms. This approach allows you to create modular and reusable components, simplifying the overall development process and making the code easier to maintain.

Lab: Write a React Native component using JSX:

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

const MyComponent = () => {
return (
<View style={styles.container}>
<Text style={styles.text}>Hello, React Native!</Text>
</View>
);
};

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
text: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});

export default MyComponent;

In this example, a simple component called MyComponent is created, which consists of a View and a Text element. The View acts as a container, and the Text element displays the text "Hello, React Native!" on the screen. The styles object is used to apply styling to the elements using the StyleSheet API provided by React Native.

Lab: Write a component to output to the android screen the output of a react native app with a button which when clicked presents a picture of a dog on the screen. Include details of the Android Simulator

Lab Notebook: Outputting Images on Android Screen with React Native and Android Simulator

React Native is a powerful framework that allows developers to build cross-platform mobile applications using JavaScript and the React library. In this lab, we will learn how to output images on the Android screen using a React Native app. We will create a simple app that displays an image of a dog when a button is clicked. We will also use the Android Simulator to test our app.

Prerequisites

Basic knowledge of JavaScript and React
Node.js installed on your computer
Android Studio installed on your computer
Android Simulator installed on your computer

Step 1: Setting up the environment

First, we need to create a new React Native project. Open a terminal and navigate to the directory where you want to create the project. Then, run the following command:
npx react-native init DogApp


This will create a new React Native project called DogApp in the current directory.

Step 2: Adding a button and an image

Open the App.js file located in the DogApp directory using a text editor. Replace the existing code with the following code:
import React, { useState } from 'react';
import { View, Button, Image } from 'react-native';

const DogApp = () => {
const [showImage, setShowImage] = useState(false);

const handleButtonPress = () => {
setShowImage(true);
};

return (
<View>
<Button title="Show Dog" onPress={handleButtonPress} />
{showImage && <Image source={require('./assets/dog.jpg')} />}
</View>
);
};

export default DogApp;

This code imports the necessary components from the react-native module and defines a functional component called DogApp.
The DogApp component contains a state variable called showImage, which is initially set to false.
When the button is clicked, the handleButtonPress function is called, which sets the showImage state variable to true.
The component also contains a conditional statement that displays an image of a dog when the showImage variable is true.

Step 3: Adding the dog image

Download an image of a dog and save it in the DogApp/assets directory with the name dog.jpg. Make sure the file name and extension match the name used in the Image component in the App.js file.

Step 4: Running the app on the Android Simulator

Open Android Studio and click on the "AVD Manager" button in the toolbar. This will open the Android Virtual Device Manager. Click on the "Create Virtual Device" button and select a device to emulate. Follow the prompts to download and install the necessary system images.
Once you have created a device, click on the "Run" button in the toolbar to start the Android Simulator. Once the simulator is running, navigate to the DogApp directory in a terminal and run the following command:
npx react-native run-android

This will build the app and install it on the Android Simulator. Once the app is installed, you should see a button labeled "Show Dog" on the screen. When you click the button, an image of a dog should appear on the screen.

Conclusion

In this lab, we learned how to output images on the Android screen using a React Native app. We created a simple app that displays an image of a dog when a button is clicked. We also used the Android Simulator to test our app. This tutorial should serve as a foundation for more complex mobile app development using React Native and the Android Simulator.

Lab: Write a component for a web application using React.js

First, make sure you have Node.js and npm (Node Package Manager) installed on your computer. You can download Node.js from the official website:
Create a new React app using the create-react-app command-line tool:
npx create-react-app my-app
cd my-app

Replace the content of the src/App.js file with the following React component:
javascriptCopy code
import React, { Component } from 'react';
import './App.css';

class HelloWorld extends Component {
render() {
return (
<div className="container">
<h1>Hello, World!</h1>
</div>
);
}
}

export default HelloWorld;

Modify the src/App.css file to style the component:
cssCopy code
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #F5FCFF;
}

h1 {
font-size: 20px;
text-align: center;
margin: 10px;
}

Replace the content of the src/index.js file with the following code to render the HelloWorld component:
javascriptCopy code
import React from 'react';
import ReactDOM from 'react-dom';
import HelloWorld from './App';
import './index.css';

ReactDOM.render(
<React.StrictMode>
<HelloWorld />
</React.StrictMode>,
document.getElementById('root')
);

Start the development server by running the following command in the terminal:

npm start

This will open your default web browser, and you should see the "Hello, World!" text displayed on the screen.
Please note that this example is for a React web application, not a React Native mobile application. React and React Native share some similarities in terms of syntax and concepts, but they are used for different purposes (web vs. mobile development).


Expo CLI is a command-line tool that helps streamline the React Native development process. The primary purpose of using Expo CLI in React Native development is for rapid prototyping and testing. Expo provides an ecosystem of tools and services that make it easy to create, build, and deploy React Native applications without having to deal with native code or platform-specific build configurations.

Some of the key features provided by Expo CLI include:
Simplified project setup and configuration.
An Expo client app for testing and previewing your app on iOS and Android devices.
Access to a wide range of pre-built components and APIs that simplify the development process.
Over-the-air updates, enabling you to push updates to your app without going through the app store review process.
A managed build service for creating standalone app binaries for iOS and Android without needing Xcode or Android Studio.
While Expo CLI simplifies the development process, it's important to note that it may not be suitable for every React Native project. Some projects may require native modules or customizations that aren't supported by Expo, necessitating the use of the React Native CLI and manual native code management.



IOS Application Development

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.