React UI Tutorial
REACT Basic
React is a front-end JavaScript library.
React was developed by Facebook Software Engineer Jordan Walke.
React is also known as React.js or ReactJS. The following image shows the relationship between the main modules in React.
Run an Existing REACT App
If you have built an application and later you have opened it with VS Code then you can start the application by
Visul Studio Code | Terminal | npm run dev or
PowerShell | the folder | npm run dev
React UI Tutorial: Buttons, Edit Boxes, Comboboxes, And Backend API Data
This tutorial focuses on React’s user interface.
We shall learn:
– What a React component is
– How to create a button
– How to create an edit box
– How to create a combobox
– How to store UI values with React state
– How to send data to a backend API
– How to read data from a backend API
The examples use your Tank Monitor backend:
text
http://127.0.0.1:5000
0. Prepare PowerShell
cd c:\TargetFolder
npm create vite@latest my-react-app -- --template react
ESLint
Yes
http://localhost:5173Now open VS Code and open the folder c:\TargetFolder\my-react-app. If you have already opened a folder then you can run
npm run devCommand | Effect on your project |
|---|---|
| Starts the React development server. Your files remain unchanged. |
| Installs dependencies into |
| Creates or updates the |
| Creates a new project. Avoid running it over an existing project. |
1. What React Does
React helps you build a user interface with small pieces called components. We can modify App.jsx in VS Code.
Example component:
jsx
function App() {
return (
Tank Monitor
); }
A component is just a JavaScript function that returns UI.
2. Important React Idea: State
State means data that can change while the page is open.
Examples:
– Text typed into an edit box
– Selected value in a combobox
– Data loaded from PostgreSQL through the backend API
– Loading or error messages
React uses ‘useState’ for this:
jsx
import { useState } from "react";
function App() {
const [tankName, setTankName] = useState("");
return (
<input
value={tankName}
onChange={(event) => setTankName(event.target.value)}
/>
);
}Meaning:
text
tankName
is the current value.
text
setTankName
changes the value.
3. Button Example
A React button looks like normal HTML, but you attach a JavaScript function with ‘onClick’.
jsx
function App() {
function myFunction() {
alert("Button clicked");
}
return (
);
}When the user presses the button, React runs:
javascript
handleButtonClick()
4. Edit Box Example
An edit box is usually an ``.
jsx
import { useState } from "react";
function App() {
const [operatorName, setOperatorName] = useState("");
return (
<input
type="text"
value={operatorName}
onChange={(event) => setOperatorName(event.target.value)}
/>
Current value: {operatorName}
);
}Important parts:
jsx
value={operatorName}The input displays the React state value.
jsx
onChange={(event) => setOperatorName(event.target.value)}When the user types, React updates the state.
This is called a controlled input.
5. Number Edit Box Example
For numbers:
jsx
import { useState } from "react";
function App() {
const [tankLevel, setTankLevel] = useState(0);
return (
<input
type="number"
value={tankLevel}
onChange={(event) => setTankLevel(Number(event.target.value))}
/>
Tank level: {tankLevel}
);
}‘event.target.value’ is normally text, so ‘Number(…)’ converts it to a number.
6. Date And Time Edit Box
For tank full and empty time:
JSX
import { useState } from "react";
function App() {
const [tankFullTime, setTankFullTime] = useState("");
return (
<input
type="datetime-local"
value={tankFullTime}
onChange={(event) => setTankFullTime(event.target.value)}
/>
{tankFullTime}
);
}The browser shows a date/time picker.
7. Combobox Example
In HTML, a combobox is a `
jsx
function App() {
const [tankStatus, setTankStatus] = useState("full");
return (
<select
value={tankStatus}
onChange={(event) => setTankStatus(event.target.value)}
>
Full
Empty
Running
Selected status: {tankStatus}
);
}
The state value is:
javascript
tankStatus
The function that changes it is:
javascript
setTankStatus
8. Loading Data From Backend API
To read tank data from your backend:
text
GET http://127.0.0.1:5000/api/tank-times
React code:
jsx
function App() {
const [tankRows, setTankRows] = useState([]);
const [error, setError] = useState("");
async function loadTankRows() {
try {
const response = await fetch("http://127.0.0.1:5000/api/tank-times");
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || "Could not load tank rows");
}
setTankRows(data);
} catch (error) {
setError(error.message);
}
}
useEffect(() => {
loadTankRows();
}, []);
return (Tank Rows
{error &&
{error}
}
{JSON.stringify(tankRows, null, 2)});
}
Important:
jsx
useEffect(() => {
loadTankRows();
}, []);This runs once when the page opens.
You might need to comment out previous imports such as import { useEffect, useState } from “react”;
9. Showing API Data In A Table
Instead of showing JSON, show a table:
jsx
import { useEffect, useState } from "react";
function App() {
const [tankRows, setTankRows] = useState([]);
const [error, setError] = useState("");
async function loadTankRows() {
setError("");
try {
const response = await fetch(
"http://127.0.0.1:5000/api/tank-times"
);
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || "Could not load tank rows");
}
setTankRows(data);
} catch (error) {
setError(error.message);
}
}
useEffect(() => {
loadTankRows();
}, []);
return (
Tank Monitoring
{error &&
{error}
}
{tankRows.map((row) => ( ))}
| ID | Tank Full Time | Tank Empty Time | Duration Minutes |
|---|---|---|---|
| {row.id} | {row.tankFullTime} | {row.tankEmptyTime || “Open”} | {row.durationMinutes ?? “Open”} |
);
}
10. Sending Data To Backend API
To save a completed tank row, call:
text
POST http://127.0.0.1:5000/api/tank-times
React example:
jsx
import { useState } from "react";
function App() {
const [tankFullTime, setTankFullTime] = useState("");
const [tankEmptyTime, setTankEmptyTime] = useState("");
const [message, setMessage] = useState("");
async function saveTankTime() {
try {
const response = await fetch("http://127.0.0.1:5000/api/tank-times", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
tankFullTime: tankFullTime,
tankEmptyTime: tankEmptyTime
})
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.error || "Could not save tank time");
}
setMessage("Saved successfully");
} catch (error) {
setMessage(error.message);
}
}
return (
<input
type=”datetime-local”
value={tankFullTime}
onChange={(event) => setTankFullTime(event.target.value)}
/>
<input
type=”datetime-local”
value={tankEmptyTime}
onChange={(event) => setTankEmptyTime(event.target.value)}
/>
{message}
);
}
11. Form Submit Example
For real forms, prefer ‘onSubmit’.
jsx
import { useState } from "react";
function App() {
const [tankFullTime, setTankFullTime] = useState("");
const [tankEmptyTime, setTankEmptyTime] = useState("");
async function handleSubmit(event) {
event.preventDefault();
await fetch("http://127.0.0.1:5000/api/tank-times", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
tankFullTime,
tankEmptyTime
})
});
}
return ( );
}
This line is important:
javascript
event.preventDefault();
It stops the browser from refreshing the page when the form is submitted.
12. Full UI Example
This example includes:
– Edit boxes
– Combobox
– Button
– Backend GET
– Backend POST
– Table
jsx
import { useEffect, useState } from "react";
function App() {
const [tankFullTime, setTankFullTime] = useState("");
const [tankEmptyTime, setTankEmptyTime] = useState("");
const [tankStatus, setTankStatus] = useState("completed");
const [rows, setRows] = useState([]);
const [message, setMessage] = useState("");
async function loadRows() {
try {
const response = await fetch(
"http://127.0.0.1:5000/api/tank-times"
);
if (!response.ok) {
throw new Error("Could not load rows");
}
const data = await response.json();
setRows(data);
} catch (error) {
setMessage(error.message);
}
}
async function saveRow(event) {
event.preventDefault();
setMessage("");
try {
const response = await fetch(
"http://127.0.0.1:5000/api/tank-times",
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
tankFullTime,
tankEmptyTime,
tankStatus
})
}
);
if (!response.ok) {
throw new Error("Could not save row");
}
setMessage("Saved successfully");
setTankFullTime("");
setTankEmptyTime("");
setTankStatus("completed");
await loadRows();
} catch (error) {
setMessage(error.message);
}
}
useEffect(() => {
loadRows();
}, []);
return (
Tank Monitor
{message}
{rows.map((row) => ( ))}
| ID | Full | Empty | Status | Duration |
|---|---|---|---|---|
| {row.id} | {row.tankFullTime} | {row.tankEmptyTime || “Open”} | {row.tankStatus} | {row.durationMinutes ?? “Open”} |
);
}
13. How UI Data Links To Backend API
The UI does not talk directly to PostgreSQL.
The UI talks to Node.js:
javascript
fetch("http://127.0.0.1:5000/api/tank-times")Node.js talks to PostgreSQL:
javascript
pool.query("SELECT ... FROM tank_times")The flow is:
text
React UI
input values
|
v
fetch()
|
v
Node.js API
|
v
PostgreSQL
14. Common UI Pattern
Most React UI screens follow this pattern:
text
1. Create state variables
2. Show input controls
3. Update state when the user types or selects
4. Press a button
5. Send state values to backend API with fetch()
6. Show result or error message
7. Reload table data
Example:
JSX
import { useState } from "react";
function App() {
const [name, setName] = useState("");
function saveName() {
console.log("Saved name:", name);
alert("Saved name: " + name);
}
return ( Enter Your Name
<input
value={name}
onChange={(event) => setName(event.target.value)}
/>
);
}
15. Quick Summary
Button:
jsx
Edit box:
JSX
setName(event.target.value)} />
Combobox:
JSX
GET from backend:
javascript
const response = await fetch("http://127.0.0.1:5000/api/tank-times");
const data = await response.json();POST to backend:
javascript
await fetch("http://127.0.0.1:5000/api/tank-times", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
tankFullTime,
tankEmptyTime
})
});