<?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: brianokothokuku</title>
    <description>The latest articles on DEV Community by brianokothokuku (@okukubrianokoth).</description>
    <link>https://dev.to/okukubrianokoth</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%2F3371605%2F4dd4f64d-835f-41f4-948d-a1f1899b3525.png</url>
      <title>DEV Community: brianokothokuku</title>
      <link>https://dev.to/okukubrianokoth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/okukubrianokoth"/>
    <language>en</language>
    <item>
      <title>Understanding useEffect in React: A Beginner’s Guide</title>
      <dc:creator>brianokothokuku</dc:creator>
      <pubDate>Sun, 20 Jul 2025 08:36:31 +0000</pubDate>
      <link>https://dev.to/okukubrianokoth/understanding-useeffect-in-react-a-beginners-guide-3h18</link>
      <guid>https://dev.to/okukubrianokoth/understanding-useeffect-in-react-a-beginners-guide-3h18</guid>
      <description>&lt;p&gt;Understanding useEffect in React&lt;br&gt;
Introduction&lt;br&gt;
If you're learning React, you’ve probably come across the useEffect hook and wondered:&lt;/p&gt;

&lt;p&gt;“Why do I need it?”&lt;/p&gt;

&lt;p&gt;“When does it run?”&lt;/p&gt;

&lt;p&gt;“What is it even doing?”&lt;/p&gt;

&lt;p&gt;In this blog post, I’ll explain what useEffect is, how it works, and show you examples using a simple React app.&lt;/p&gt;

&lt;p&gt;What is useEffect?&lt;br&gt;
useEffect is a React hook that lets you run code after the component renders. It's used for handling side effects — operations that are not directly related to rendering UI, such as:&lt;/p&gt;

&lt;p&gt;Fetching data from an API&lt;/p&gt;

&lt;p&gt;Updating the DOM manually&lt;/p&gt;

&lt;p&gt;Starting or cleaning up timers&lt;/p&gt;

&lt;p&gt;Subscribing to services (e.g., websockets)&lt;/p&gt;

&lt;p&gt;Think of useEffect as "do something after the component appears or updates."&lt;/p&gt;

&lt;p&gt;Basic Syntax&lt;br&gt;
jsx&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;/p&gt;

&lt;p&gt;import { useEffect } from 'react';&lt;/p&gt;

&lt;p&gt;useEffect(() =&amp;gt; {&lt;br&gt;
  // Your code here (side effect)&lt;br&gt;
}, [dependencies]);&lt;br&gt;
The first argument is a function — the code you want to run.&lt;/p&gt;

&lt;p&gt;The second argument is a dependency array — it controls when the effect runs.&lt;/p&gt;

&lt;p&gt;Example 1: Logging on Component Mount&lt;br&gt;
jsx&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
import React, { useEffect } from 'react';&lt;/p&gt;

&lt;p&gt;function Hello() {&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    console.log("Hello component mounted!");&lt;br&gt;
  }, []);&lt;/p&gt;

&lt;p&gt;return &lt;/p&gt;
&lt;h1&gt;Hello, useEffect!&lt;/h1&gt;;&lt;br&gt;
}&lt;br&gt;
Explanation:&lt;br&gt;
The console.log runs once when the component mounts.&lt;br&gt;
The empty array [] tells React to run this effect only once, just like componentDidMount in class components.

&lt;p&gt;Example 2: Fetching Data with useEffect&lt;br&gt;
jsx&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;/p&gt;

&lt;p&gt;import React, { useState, useEffect } from 'react';&lt;/p&gt;

&lt;p&gt;function UserList() {&lt;br&gt;
  const [users, setUsers] = useState([]);&lt;/p&gt;

&lt;p&gt;useEffect(() =&amp;gt; {&lt;br&gt;
    fetch("&lt;a href="https://jsonplaceholder.typicode.com/users%22" rel="noopener noreferrer"&gt;https://jsonplaceholder.typicode.com/users"&lt;/a&gt;)&lt;br&gt;
      .then((res) =&amp;gt; res.json())&lt;br&gt;
      .then((data) =&amp;gt; setUsers(data));&lt;br&gt;
  }, []);&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
    &lt;/p&gt;
&lt;br&gt;
      &lt;h2&gt;User List&lt;/h2&gt;
&lt;br&gt;
      &lt;ul&gt;

        {users.map(user =&amp;gt; (
          &lt;li&gt;{user.name}&lt;/li&gt;

        ))}
      &lt;/ul&gt;
&lt;br&gt;
    &lt;br&gt;
  );&lt;br&gt;
}&lt;br&gt;
Explanation:&lt;br&gt;
This example shows how to fetch and display data using useEffect. The fetch call runs once on component mount and updates the state with the received user data.

&lt;p&gt;Example 3: Cleanup Function&lt;br&gt;
jsx&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;/p&gt;

&lt;p&gt;useEffect(() =&amp;gt; {&lt;br&gt;
  const interval = setInterval(() =&amp;gt; {&lt;br&gt;
    console.log("Running interval...");&lt;br&gt;
  }, 1000);&lt;/p&gt;

&lt;p&gt;return () =&amp;gt; {&lt;br&gt;
    clearInterval(interval); // Cleanup on unmount&lt;br&gt;
  };&lt;br&gt;
}, []);&lt;/p&gt;

&lt;p&gt;Explanation:&lt;br&gt;
The return inside useEffect is a cleanup function. It's used to clean up side effects when the component unmounts or before the effect runs again.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
useEffect is one of the most powerful hooks in React.&lt;br&gt;
You’ll use it for everything from fetching data to handling side effects like timers and subscriptions.&lt;/p&gt;

&lt;p&gt;It may seem tricky at first, but once you understand the dependency array and when effects run, it becomes much easier to manage.&lt;/p&gt;

&lt;p&gt;Try creating your components using useEffect — it's a great way to reinforce your learning and grow as a React developer.&lt;/p&gt;

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