1 of 29

Web Programming with React JS

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

Training Material

​

2 of 29

Prerequisites

    • Knowledge of HTML, CSS, JavaScript, and Git
    • Knowledge of package management with Node.js and npm
    • Node.js and npm locally installed
    • A code editor, such as Visual Studio Code

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

2

3 of 29

Timeline of front-end JavaScript frameworks

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

3

jQuery* (2006)

AngularJS (2010)

React (2013)

Vue (2014)

Angular (2014)

* jQuery is more often considered a library than a framework

4 of 29

Core Concepts

​

  • Components
  • JSX
  • State and Props
  • Virtual DOM
  • Lifecycle Methods
  • Hooks

​

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

4

5 of 29

Introduction to React

  • React is an open-source front-end framework
  • React was created by Jordan Walke, a software engineer at Facebook
  • React introduces JSX, or JavaScript XML
  • React JS, commonly known as React, is a JavaScript library for building user interfaces.
  • React can be used as a base in the development of single-page or mobile applications.
  • React applications requires the use of additional libraries for state management and routing.

​

​

ELT layout

6 of 29

Introduction to React

  • Over the years, React has evolved with significant milestones, including the introduction of the virtual DOM, hooks, and concurrent mode.
  • React JS Virtual DOM is an in-memory representation of the DOM.
  • DOM refers to the Document Object Model that represents the content of XML or HTML documents as a tree structure
  • The virtual DOM allows React to perform updates in a very efficient manner by comparing the new DOM with the previous one and only updating the parts that have changed.

​

​

​

ELT layout

7 of 29

Introduction to JSX

  • JSX allows HTML (XML) and JavaScript to be combined in one file
  • Allows for faster development
  • JSX follows XML rules
    • All elements must be placed inside one parent element
    • All elements must be closed
  • Browsers do not natively support JSX
    • JSX must be converted to HTML and JavaScript through the build process
    • Several tools exist for managing this process including Vite, Webpack and Snowpack

ELT layout

8 of 29

JavaScript and HTML in the same file

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

8

Traditional approach

React approach

HTML

CSS

JS

JSX

CSS or JSS

9 of 29

Understanding JSX

1. Embedding Expressions: You can embed JavaScript expressions within JSX by wrapping them in curly braces `{}`.

For example:

const name = 'React';

const element = <h1>Hello, {name}!</h1>;

​

2. Attributes: JSX allows you to use attributes similar to HTML. For example, you can set the class of an element using 'className' instead of 'class’.� const element = <div className="my-class">Content</div>;

​

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

9

10 of 29

Understanding JSX

3. Specifying Children: You can specify child elements within a tag by placing them inside the opening and closing tags:� const element = (<div><h1>Hello, world!</h1>

<p>Welcome to learning React.</p> </div>);

4. JSX Prevents Injection Attacks: JSX automatically escapes any values embedded within it to prevent injection attacks. This means that it is safe to include user input in your JSX:

const title = response.potentiallyMaliciousInput;

// This is safe:

const element = <h1>{title}</h1>;

​

​

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

10

11 of 29

Components

  • React development is based on components
  • Components are self-contained, reusable units of UI and logic
  • React projects typically contain many components
  • While React components can be a function or a class

ELT layout

12 of 29

Components

Cart component

ProductList component

Product component

Product component

ELT layout

13 of 29

The application host

  • Every React app will contain an HTML file to host the app
  • Note the div element with the id app

<!DOCTYPE html>�<html lang="en">�<head>� <title>Recipes</title>�</head>�<body>� <div id="app"></div>� <script type="module" src="/dist/index.js"></script>�</body>�</html>

HTML

ELT layout

14 of 29

index.js

  • React apps often use index.js as the root to the project
  • This will typically load the React app and place it on the page

import React from 'react';�import ReactDOM from 'react-dom';��ReactDOM.render(� <h1>Hello, world!</h1>,� document.getElementById('app')�);

JSX

ELT layout

15 of 29

The core component

Many React projects start with one core component called App

import React from 'react';��function App() {� return (� <article>� <h1>Recipe Manager</h1>� </article>� )�}��export default App;

JSX

ELT layout

16 of 29

Anatomy of a React component

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

16

export default function MyComponent(props) {

return <div>Hello, world! My name is {props.name}</div>;

}

const html = <MyComponent name="aaron" />;

Inputs are passed through a single argument called “props”

The function is executed as if it was an HTML tag

The function outputs HTML

The component is just a function

Parameters are passed in as HTML attributes

17 of 29

Anatomy of a new React app

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

17

public holds the initial html document and other static assets

​

​

App is a boilerplate starter component

​

​

index.js binds React to the DOM

​

package.json configures npm dependencies

18 of 29

State and Props in React

State:

  • An object that holds dynamic data for a component.
  • Allows components to manage and update their own data.

class MyComponent extends React.Component {� constructor(props) {� super(props);� this.state = { count: 0 };� }}

Props:

  • Short for properties, used to pass data from parent to child components.
  • Read-only and immutable.

function ChildComponent(props) {� return <h1>{props.message}</h1>;�}��function ParentComponent() {� return <ChildComponent message="Hello, World!" />;�}�

19 of 29

Virtual DOM

What is the Virtual DOM?

  • A lightweight representation of the real DOM.
  • React uses it to optimize and batch updates.

How it works:

  • When state or props change, a new Virtual DOM is created.
  • React compares the new Virtual DOM with the previous one .
  • Only the differences are updated in the real DOM.

Benefits:

  • Improved performance.
  • Efficient updates and rendering.

20 of 29

React Lifecycle Methods

Mounting:

  • componentDidMount(): Called after the component is inserted into the DOM.

Updating:

  • componentDidUpdate(prevProps, prevState): Called after the component's updates are flushed to the DOM.

Unmounting:

  • componentWillUnmount(): Called right before the component is removed from the DOM.

Error Handling:

  • componentDidCatch(error, info): Called when there is an error during rendering, in a lifecycle method, or in the constructor of any child component.

21 of 29

React Hooks

  • Functions that let you use state and other React features in functional components.
  • Introduced in React 16.8.

Common Hooks:

useState(): Manages state in functional components.

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

useEffect(): Manages side effects like data fetching, subscriptions, and manual DOM manipulations.

useContext(): Accesses the context API.

const value = useContext(MyContext);

Custom Hooks:

- Create reusable hooks by extracting component logic into a function.

function useCustomHook() {

// Custom logic

}�

22 of 29

Communicating between React components

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

22

  • Passing information from parent to child: Use props (attributes)

​

<ChildComponent param={infoForChildComponent} />

​

  • Passing information from child to parent: Callbacks

​

this.parentCallback = (infoFromChild) =>

{ /* processInfoFromChild */};

​

<ChildComponent callback={this.parentCallback}> />

​

23 of 29

Getting Started with React: Setting Up the Environment

​

To start building applications with React, you need to set up your development environment. Here are the steps to get you started:

1. Install Node.js and npm: React is built on JavaScript, so you'll need Node.js and npm (Node Package Manager) installed on your machine. You can download and install them from the official Node.js website (https://nodejs.org/).�https://www.freecodecamp.org/news/how-to-install-react-a-step-by-step-guide/

​

2. Create a React Application: The easiest way to create a React application is by using the Create React App (CRA) tool, which sets up everything you need with a single command. Open your terminal and run the following command:

npx create-react-app my-app

cd my-app

npm start

This will create a new React application in a folder named `my-app` and start a local development server.

​

​

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

23

24 of 29

Getting Started with React: Setting Up the Environment

3. Code Editor: You'll need a code editor to write your React code. Visual Studio Code (VS Code) is a popular choice among developers due to its rich feature set and extensive plugin ecosystem.

​

4. Browser Developer Tools: Modern browsers come with developer tools that allow you to inspect the DOM, debug JavaScript, and analyze network activity. Chrome and Firefox are widely used and have robust developer tools.

​

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

24

25 of 29

Creating Simple Project

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

25

26 of 29

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

26

Advantages of React

  • Speed
  • Flexibility
  • Performance
  • Usability
  • Reusable Components
  • It helps to build rich user interfaces
  • It allows writing custom components

27 of 29

Online React Compilation Tools

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

27

28 of 29

Exercise

Create an Form using react JS

Reference : Create a Form using React JS - GeeksforGeeks

ELT layout

29 of 29

Resources

  • Official Documentation

React Documentation : https://reactjs.org/docs/getting-started.html

  • Tutorials and Courses

React Tutorial : https://reactjs.org/tutorial/tutorial.html

​

REU Site: Applying Data Science on Energy-efficient Cluster Systems and Applications

29