<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ishaan Malhotra</title>
    <description>The latest articles on DEV Community by Ishaan Malhotra (@ishaanmalhotra).</description>
    <link>https://dev.to/ishaanmalhotra</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3881821%2F0763cd9f-72c7-4bde-8e7b-76d637ed7d6f.png</url>
      <title>DEV Community: Ishaan Malhotra</title>
      <link>https://dev.to/ishaanmalhotra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ishaanmalhotra"/>
    <language>en</language>
    <item>
      <title>How to build a quick orders page using an API</title>
      <dc:creator>Ishaan Malhotra</dc:creator>
      <pubDate>Thu, 16 Apr 2026 07:00:05 +0000</pubDate>
      <link>https://dev.to/ishaanmalhotra/how-to-receive-orders-from-an-api-21no</link>
      <guid>https://dev.to/ishaanmalhotra/how-to-receive-orders-from-an-api-21no</guid>
      <description>&lt;p&gt;In modern e-commerce applications, the "Order History" page is a vital touchpoint. It requires a blend of authentication logic, asynchronous data fetching, and clean UI presentation.&lt;/p&gt;

&lt;p&gt;Below is an article-style breakdown of how to build a robust "My Orders" component using React and React Router.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setting the Foundation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every React component begins with its imports. Here, we bring in the necessary hooks for state and side effects, as well as useNavigate for handling user redirection.&lt;br&gt;
JavaScript&lt;/p&gt;

&lt;p&gt;import { useState, useEffect } from 'react'&lt;br&gt;
import { useNavigate } from 'react-router-dom'&lt;br&gt;
import './MyOrders.css'&lt;/p&gt;

&lt;p&gt;function MyOrders({ user }) {&lt;br&gt;
  const navigate = useNavigate()&lt;br&gt;
  const [orders, setOrders] = useState([])&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Implementing the Security Guard&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We don't want unauthorized users accessing order data. We use the useEffect hook to check if a user exists. If not, we programmatically move them to the login page before any data is requested.&lt;br&gt;
JavaScript&lt;/p&gt;

&lt;p&gt;useEffect(() =&amp;gt; {&lt;br&gt;
    if (!user) { &lt;br&gt;
      navigate('/login')&lt;br&gt;
      return &lt;br&gt;
    }&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const load = async () =&amp;gt; {
  const res  = await fetch(`/api/orders/${user.customerID}`)
  const data = await res.json()
  setOrders(data)
}
load()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}, [user])&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Handling the "Loading" or "Unauthorized" State&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Even though we redirect in the background, we need a "fallback" UI to prevent the component from trying to render properties of a null user, which would crash the app.&lt;br&gt;
JavaScript&lt;/p&gt;

&lt;p&gt;if (!user) return &lt;/p&gt;
&lt;p&gt;Please log in.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Presentation Layer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once the data is fetched, we use the .map() function to iterate through the orders array. We also include a "Conditional Rendering" check to show a friendly message if the user hasn't bought anything yet.&lt;br&gt;
JavaScript&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
    &lt;/p&gt;
&lt;br&gt;
      &lt;h1&gt;My Orders&lt;/h1&gt;
&lt;br&gt;
      {orders.length === 0 &amp;amp;&amp;amp; &lt;p&gt;No orders yet.&lt;/p&gt;}&lt;br&gt;
      {orders.map(order =&amp;gt; (&lt;br&gt;
        &lt;br&gt;
          &lt;p&gt;{order.product_name}&lt;/p&gt;
&lt;br&gt;
          &lt;p&gt;Quantity: {order.product_quantity}&lt;/p&gt;
&lt;br&gt;
          &lt;p&gt;Date: {new Date(order.order_date).toLocaleDateString()}&lt;/p&gt;
&lt;br&gt;
        &lt;br&gt;
      ))}&lt;br&gt;
    &lt;br&gt;
  )&lt;br&gt;
}

&lt;p&gt;export default MyOrders&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Styling for Readability&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Clean code deserves a clean interface. The CSS focuses on "scannability"—making it easy for the user to distinguish between different orders at a glance using cards and specific color accents.&lt;br&gt;
CSS&lt;/p&gt;

&lt;p&gt;.page {&lt;br&gt;
  padding: 30px;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.order-card {&lt;br&gt;
  border: 1px solid #ccc;&lt;br&gt;
  border-radius: 8px;&lt;br&gt;
  padding: 16px;&lt;br&gt;
  margin-bottom: 10px;&lt;br&gt;
  max-width: 500px;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.order-product {&lt;br&gt;
  font-weight: 700;&lt;br&gt;
  margin-bottom: 8px;&lt;br&gt;
  color: #144A5C; /* A professional slate-blue for product titles */&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Summary of Techniques Used&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Redirect Logic: Utilizing useNavigate to protect private routes.

Data Hydration: Fetching user-specific data via customerID.

User Experience: Formatting raw ISO dates into localized, readable strings (e.g., "1/12/2024").

Scalability: Using key props on mapped elements to ensure React manages the list efficiently.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>api</category>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
