Introduction
React, the JavaScript library for building interactive and attractive UIs, is very famous these days. There are many articles on learning React and creating a React application. However, today, I would like to showcase a simple application skeleton in which we create a simple React front-end UI and connect it to a .NET core Web API to get the data.
React front-end application
I have created a simple React application using Vite. I would recommend you look into this framework for building JavaScript framework front-end applications here.
After creating the initial application using React and TypeScript, you can update/create the components as below. Bootstrap was added for styling. I am using Visual Studio Code for designing and developing the application.
//Main.tsximport React from 'react'
import ReactDOM from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import App from './App'
import './index.css'
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>,
)
//App.tsximport { Link, Route, Routes } from 'react-router-dom'
import Home from './components/Home'
import Employees from './components/Employees'
import AddEmployee from './components/AddEmployee'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.min.js'
const App = () => {
return (
<>
<nav className="navbar bg-body-tertiary">
<div>
<a className="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Main Menu
</a>
<ul className="dropdown-menu">
<li><a className="dropdown-item" href="/">Home</a></li>
<li><a className="dropdown-item" href="/employees">Employees</a></li>
<li><a className="dropdown-item" href="/addemployee">Add Employee</a></li>
</ul>
</div>
</nav>
<p />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/employees" element={<Employees />} />
<Route path="/addemployee" element={<AddEmployee />} />
<Route />
</Routes>
</>
)}
export default App
//Home.tsx
const Home = () => {
return <h1 className="text-center">Home</h1>
};
export default Home;
//Employees.tsx
import axios from 'axios';
import { useEffect, useState } from "react";
import url from "../misc/fixed";
interface Employee{
id: number,
name: string,
age:number,
counter:number
}
const Employees = () => {
const [employees, setEmployees] = useState<Employee[]>([]);
useEffect(() => {
getEmployees();
},[])
const getEmployees = () => {
axios.get<Employee[]>(url)
.then(res => setEmployees(res.data));
}
const onDelete = (id: number) =>
{
axios.delete(url + `/${id}`)
.then(res=> console.log(res.data)) // getEmployees()
.catch(err=> console.log(err));
setEmployees(employees.filter((e) => e.id != id)); // This is temporary
}
if (employees.length == 0) return (
<>
<h1 className="text-center">Employees</h1>
<p/>
<h3>There are no employees</h3>
</>
);
return (
<>
<h1 className="text-center">Employees</h1>
<p/>
<div className="container text-center">
<div className="row">
<div className="col" style={{backgroundColor: "darkblue", color:"white"}}>
Employee Id
</div>
<div className="col" style={{backgroundColor: "darkblue", color:"white"}}>
Employee Name
</div>
<div className="col" style={{backgroundColor: "darkblue", color:"white"}}>
Employee age
</div>
<div className="col" style={{backgroundColor: "darkblue", color:"white"}}>
</div>
</div>
{employees.map((employee)=> (
<div className="row" key={employee.id}>
<div className= {employee.counter%2 == 0 ? "col p-3 mb-2 bg-info text-dark" : "col p-3 mb-2 bg-light text-dark"}>
{employee.id}
</div>
<div className={employee.counter%2 == 0 ? "col p-3 mb-2 bg-info text-dark" : "col p-3 mb-2 bg-light text-dark"}>
{employee.name}
</div>
<div className={employee.counter%2 == 0 ? "col p-3 mb-2 bg-info text-dark" : "col p-3 mb-2 bg-light text-dark"}>
{employee.age}
</div>
<div className={employee.counter%2 == 0 ? "col p-3 mb-2 bg-info text-dark" : "col p-3 mb-2 bg-light text-dark"}>
<button className="btn btn-outline-danger" onClick={() => onDelete(employee.id)}>
Delete
</button>
</div>
</div>
))}
</div>
</>
)};
export default Employees;





Join the conversation! Your thoughts help the community grow.