<?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: Brian Cheruiyot</title>
    <description>The latest articles on DEV Community by Brian Cheruiyot (@briancheruiyot11).</description>
    <link>https://dev.to/briancheruiyot11</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%2F3305051%2Fa6aaaa8e-7f7e-40bf-bcb1-a68296da5db0.jpg</url>
      <title>DEV Community: Brian Cheruiyot</title>
      <link>https://dev.to/briancheruiyot11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/briancheruiyot11"/>
    <language>en</language>
    <item>
      <title>How to Fetch API Data in React Using useEffect</title>
      <dc:creator>Brian Cheruiyot</dc:creator>
      <pubDate>Sun, 27 Jul 2025 15:03:40 +0000</pubDate>
      <link>https://dev.to/briancheruiyot11/how-to-fetch-api-data-in-react-using-useeffect-4o8c</link>
      <guid>https://dev.to/briancheruiyot11/how-to-fetch-api-data-in-react-using-useeffect-4o8c</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1ptzb02lvxpbz7wcp95.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1ptzb02lvxpbz7wcp95.jpg" alt=" " width="800" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fetching data from an API is a common task in &lt;strong&gt;React&lt;/strong&gt;  applications. The easiest way to handle this in functional components is by using the &lt;strong&gt;&lt;code&gt;useEffect&lt;/code&gt;&lt;/strong&gt; hook along with &lt;strong&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this steps, we’ll learn how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetch API data in React
&lt;/li&gt;
&lt;li&gt;Show loading and error states
&lt;/li&gt;
&lt;li&gt;Render the data on the screen
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of &lt;strong&gt;React&lt;/strong&gt; and &lt;strong&gt;JavaScript&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Node.js installed on your machine&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  1. Create a New React App
&lt;/h2&gt;

&lt;p&gt;You can start with &lt;strong&gt;Create React App&lt;/strong&gt; or any React boilerplate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app fetch-demo
cd fetch-demo
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Setup the Component
&lt;/h2&gt;

&lt;p&gt;We’ll fetch data from the JSONPlaceholder API (a free test API).&lt;/p&gt;

&lt;p&gt;Here’s the basic setup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from "react";

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

  useEffect(() =&amp;gt; {
    fetch("https://jsonplaceholder.typicode.com/posts")
      .then((response) =&amp;gt; response.json())
      .then((data) =&amp;gt; setPosts(data));
  }, []);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Blog Posts&amp;lt;/h1&amp;gt;
      &amp;lt;ul&amp;gt;
        {posts.map((post) =&amp;gt; (
          &amp;lt;li key={post.id}&amp;gt;{post.title}&amp;lt;/li&amp;gt;
        ))}
      &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What’s happening?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;useState([])&lt;/code&gt;&lt;/strong&gt; → Stores the posts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;useEffect()&lt;/code&gt;&lt;/strong&gt; → Runs after the component renders, fetching the data once.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;fetch()&lt;/code&gt;&lt;/strong&gt; → Gets the data and updates the state.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Add Loading and Error States
&lt;/h2&gt;

&lt;p&gt;To make the UI better, let’s add loading and error handling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from "react";

function App() {
  const [posts, setPosts] = useState([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() =&amp;gt; {
    fetch("https://jsonplaceholder.typicode.com/posts")
      .then((response) =&amp;gt; {
        if (!response.ok) {
          throw new Error("Failed to fetch data");
        }
        return response.json();
      })
      .then((data) =&amp;gt; setPosts(data))
      .catch((err) =&amp;gt; setError(err.message))
      .finally(() =&amp;gt; setLoading(false));
  }, []);

  if (loading) return &amp;lt;p&amp;gt;Loading...&amp;lt;/p&amp;gt;;
  if (error) return &amp;lt;p&amp;gt;Error: {error}&amp;lt;/p&amp;gt;;

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Blog Posts&amp;lt;/h1&amp;gt;
      &amp;lt;ul&amp;gt;
        {posts.map((post) =&amp;gt; (
          &amp;lt;li key={post.id}&amp;gt;{post.title}&amp;lt;/li&amp;gt;
        ))}
      &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Now your app:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Shows Loading... while data is fetched&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Displays an error message if something goes wrong&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You’ve successfully built a React component that fetches data from an API using useEffect and handles loading and error states. This pattern is essential for any real-world React application.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Fetching Data from APIs in JavaScript Made Easy</title>
      <dc:creator>Brian Cheruiyot</dc:creator>
      <pubDate>Sun, 29 Jun 2025 09:26:18 +0000</pubDate>
      <link>https://dev.to/briancheruiyot11/fetching-data-from-apis-in-javascript-made-easy-3mgj</link>
      <guid>https://dev.to/briancheruiyot11/fetching-data-from-apis-in-javascript-made-easy-3mgj</guid>
      <description>&lt;p&gt;Have you ever wanted your website or app to display real-time information, such as weather updates, random quotes, or user data? If so, you're thinking about using an API.&lt;/p&gt;

&lt;p&gt;An API (Application Programming Interface) lets your application communicate with another service to send or receive data. In JavaScript, the easiest way to work with APIs is by using the built-in fetch() method.&lt;/p&gt;

&lt;p&gt;Let’s explore how it works in a simple and beginner-friendly way.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 What Is fetch()?
&lt;/h2&gt;

&lt;p&gt;The fetch() function allows you to send HTTP requests (like GET, POST, etc.) to a server and get a response, usually in JSON format.&lt;/p&gt;

&lt;p&gt;Here's the basic syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://api.example.com/data')
  .then(response =&amp;gt; response.json())
  .then(data =&amp;gt; console.log(data))
  .catch(error =&amp;gt; console.error('Error:', error));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔍 Let’s Understand Each Step
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;fetch('https://api.example.com/data')&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sends a GET request to the specified URL.&lt;/p&gt;

&lt;p&gt;2.&lt;code&gt;.then(response =&amp;gt; response.json())&lt;/code&gt;&lt;br&gt;
Converts the response into a JavaScript object.&lt;/p&gt;

&lt;p&gt;3.&lt;code&gt;.then(data =&amp;gt; console.log(data))&lt;/code&gt;&lt;br&gt;
You now have the data! You can display it or use it however you like.&lt;/p&gt;

&lt;p&gt;4.&lt;code&gt;.catch(error =&amp;gt; console.error('Error:', error))&lt;/code&gt;&lt;br&gt;
Catches any errors, such as a network issue or an invalid URL.&lt;/p&gt;
&lt;h2&gt;
  
  
  🧪 Real Example: Getting Random User Data
&lt;/h2&gt;

&lt;p&gt;Let’s use a real API that gives us random user information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://randomuser.me/api/')
  .then(response =&amp;gt; response.json())
  .then(data =&amp;gt; {
    const user = data.results[0];
    console.log(`Name: ${user.name.first} ${user.name.last}`);
  })
  .catch(error =&amp;gt; console.error('Something went wrong:', error));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✉️ Sending Data with a POST Request
&lt;/h2&gt;

&lt;p&gt;Sometimes, you’ll want to send data to an API, like when submitting a form. Here's how to do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://jsonplaceholder.typicode.com/posts', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    title: 'Hello World',
    body: 'This is a blog post',
    userId: 1,
  }),
})
  .then(response =&amp;gt; response.json())
  .then(data =&amp;gt; console.log('Post created:', data))
  .catch(error =&amp;gt; console.error('Error:', error));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✨ Cleaner Code with async/await
&lt;/h2&gt;

&lt;p&gt;A modern and more readable way to fetch data is using async and await:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function getUser() {
  try {
    const response = await fetch('https://randomuser.me/api/');
    const data = await response.json();
    const user = data.results[0];
    console.log(`User: ${user.name.first} ${user.name.last}`);
  } catch (error) {
    console.error('Error fetching user:', error);
  }
}

getUser();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  💡 Tips for Working with APIs
&lt;/h2&gt;

&lt;p&gt;✅ Always handle errors with .catch() or try...catch.&lt;br&gt;
✅ Use browser DevTools (Network tab) to inspect requests and responses.&lt;br&gt;
✅ Many free APIs are available for practice, such as:&lt;/p&gt;

&lt;h3&gt;
  
  
  Dog API
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://dog.ceo/dog-api/" rel="noopener noreferrer"&gt;https://dog.ceo/dog-api/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🚀 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;APIs are powerful tools that bring your websites and apps to life with real-world data. Whether you're building a news feed, a weather app, or a fun fact generator, fetch() makes it easy to get started.&lt;/p&gt;

&lt;p&gt;Start experimenting with your favorite APIs and turn your JavaScript skills into something dynamic and interactive! 💻⚡&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
