Node.js  

Getting Started with React: Setting Up Your Project and Integrating APIs

Introduction

React has quickly become one of the most popular JavaScript libraries for building dynamic and responsive user interfaces. Developed and maintained by Facebook, React allows developers to create reusable UI components, manage state effectively, and build powerful single-page applications with ease.

In today’s web development landscape, integrating APIs is a key aspect of building interactive and data-driven applications. Whether you’re pulling data from a third-party service or communicating with your own backend, React provides a flexible and efficient way to handle API requests.

This article will guide you through the essential steps to set up your first React project and integrate APIs to fetch and display dynamic content. By the end, you’ll have a solid foundation for building scalable React applications that can connect to the outside world.

Step 1. Set Up Your React Environment

Before starting, ensure you have the following installed:

  • Node.js (v14 or higher): Download Node.js
  • npm (comes with Node.js) or yarn
  • A code editor like VS Code Create a React App

1. Open VS Code Terminal

  1. Launch Visual Studio Code.
  2. Open the terminal by clicking:
  3. Menu → Terminal → New Terminal
  4. Or press Ctrl + ~ (backtick) to open it quickly.

2. Create a React App

Use create-react-app to quickly bootstrap your project:

npx create-react-app react-api-firstdemo
cd react-api-firstdemo
npm start

This command creates a fully functional React app and starts the development server at http://localhost:3000.

3. Run the React Project

npm start

4. Project Structure Overview

Project structure

Step 2. Create a Basic API Call in React

Let's call a public API like JSON Placeholder to fetch data and display it in the app.

1. Update App.js

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

function App() {
  const [posts, setPosts] = useState([]);

  useEffect(() => {
    // API call
    fetch('http://localhost:8001/api/employee/create')
      .then((response) => response.json())
      .then((data) => setPosts(data))
      .catch((error) => console.error('Error fetching data:', error));
  }, []);

  return (
    <div className="App">
      <h1>Posts from API</h1>
      <ul>
        {posts.slice(0, 10).map((post) => (
          <li key={post.id}>
            <strong>{post.title}</strong>
            <p>{post.body}</p>
          </li>
        ))}
      </ul>
    </div>
  );
}

export default App;

2. Update App.css

.App {
  font-family: Arial, sans-serif;
  padding: 20px;
}

li {
  margin-bottom: 20px;
  list-style: none;
  border-bottom: 1px solid #ccc;
  padding-bottom: 10px;
}

Optional. Use Axios for API Requests

Instead of fetch, you can use Axios:

npm install axios

Then modify your API call in App.js:

import axios from 'axios';

useEffect(() => {
  axios.get('http://localhost:8001/api/employee/create')
    .then((response) => setPosts(response.data))
    .catch((error) => console.error(error));
}, []);

Tips for Real-World APIs

  • Use async/await for cleaner syntax.
  • Handle loading and error states.
  • Store API URLs and keys in environment variables (.env).
  • Consider using context or Redux for global state.

Conclusion

Setting up React and integrating it with APIs is a crucial step in becoming a proficient modern web developer. Mastering GET requests lays the groundwork for more advanced operations such as POST, PUT, and DELETE, enabling full interaction with backend services. As you grow more comfortable, you'll be able to handle forms, manage user authentication, and build dynamic, full-featured applications with confidence.