Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (50)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

JavaScriptProgrammingReactJS

Part 2 : Contact Management System with Node.js, Express and ReactJS

By Admin
February 3, 2025 2 Min Read
0

In our previous article, we created a REST API for a Contact Management System using Node.js and stored contacts in a JSON file. Now, let’s integrate this API with a React frontend to build a fully functional Contact Manager application.

Go To : Part 1 : Contact Management System with Node.js, Express and ReactJS

Follow this video for complete guidance :

Setting Up the React Frontend

We will use React with Axios to fetch, add, update, delete, and export contacts. Ensure that you have Node.js installed and create a new React project:

npm create vite
cd projectname
npm install axios
npm run dev

Connecting to the API

Modify App.js to integrate our backend API. First, import the required dependencies:

import { useEffect, useState } from 'react';
import axios from 'axios';
import './App.css';

Define the API base URL:

const API_URL = 'http://localhost:3000/contacts';

Fetching Contacts from the API

Use useEffect to fetch the contacts when the component mounts:

const fetchContacts = async () => {
  try {
    const response = await axios.get(API_URL);
    setContacts(response.data);
  } catch (error) {
    console.error("Error fetching contacts:" + error);
  }
};

useEffect(() => {
  fetchContacts();
}, []);

Add or Update Contacts

const addOrUpdateContact = async (e) => {
  e.preventDefault();
  try {
    if (editId) {
      await axios.patch(`${API_URL}/${editId}`, form);
    } else {
      await axios.post(API_URL, form);
    }
    setForm({ name: "", phone: "", email: '', bookmark: false });
    setEditId(null);
    fetchContacts();
  } catch (error) {
    console.error("Error saving contacts:" + error);
  }
};

Delete a Contact

const deleteContact = async (id) => {
  try {
    await axios.delete(`${API_URL}/${id}`);
    fetchContacts();
  } catch (error) {
    console.error("Error deleting contact:" + error);
  }
};

Export Contacts

const exportContacts = async () => {
  try {
    const response = await axios.get(`${API_URL}/export`, { responseType: "blob" });
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute("download", "contacts.zip");
    document.body.appendChild(link);
    link.click();
  } catch (error) {
    console.error("Error exporting contacts:" + error);
  }
};

Building the UI

We create a simple form for adding/updating contacts:

<form onSubmit={addOrUpdateContact}>
  <input type="text" name="name" onChange={handleInputChange} value={form.name} required />
  <input type="text" name="phone" onChange={handleInputChange} value={form.phone} required />
  <input type="email" name="email" onChange={handleInputChange} value={form.email} required />
  <button type="submit">Save Contact</button>
</form>
<button onClick={exportContacts}>Export Contacts</button>

Each contact is displayed in a card with edit and delete buttons:

{contacts.map(contact => (
  <div key={contact.id}>
    <h5>{contact.name}</h5>
    <p>{contact.phone}</p>
    <p>{contact.email}</p>
    <button onClick={() => editContact(contact)}>Edit</button>
    <button onClick={() => deleteContact(contact.id)}>Delete</button>
  </div>
))}

With this setup, our contact management system is fully functional, allowing users to manage their contacts efficiently. In future articles, we will explore features like authentication and cloud storage integration.

Tags:

contact managementexpressnode jsreactjs
Author

Admin

Follow Me
Other Articles
Previous

Part 1 : Contact Management System with Node.js, Express and ReactJS

Next

Instagram API to fetch User Profile, Followers, Following

No Comment! Be the first one.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS
  • Real-time Speech to Text Typing Tool using JavaScript

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme