<?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>Forem: Saleh Ahmed Mahin</title>
    <description>The latest articles on Forem by Saleh Ahmed Mahin (@salehahmedmahin).</description>
    <link>https://forem.com/salehahmedmahin</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%2F3546266%2F23bd0222-f503-47d9-9074-25557bed1c54.png</url>
      <title>Forem: Saleh Ahmed Mahin</title>
      <link>https://forem.com/salehahmedmahin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/salehahmedmahin"/>
    <language>en</language>
    <item>
      <title>The Redux Mystery: Why Does It Feel So Hard?</title>
      <dc:creator>Saleh Ahmed Mahin</dc:creator>
      <pubDate>Mon, 06 Oct 2025 19:31:34 +0000</pubDate>
      <link>https://forem.com/salehahmedmahin/the-redux-mystery-why-does-it-feel-so-hard-31do</link>
      <guid>https://forem.com/salehahmedmahin/the-redux-mystery-why-does-it-feel-so-hard-31do</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%2Fobilsdrjd63g4e28q2yn.png" 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%2Fobilsdrjd63g4e28q2yn.png" alt="React Redux Toolkit Logo" width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux!
&lt;/h2&gt;

&lt;p&gt;Just hearing the name can make new developers nervous. Why all the &lt;strong&gt;"boilerplate"&lt;/strong&gt;? Why so much setup and so many strange rules just to build a simple counter app? &lt;strong&gt;Seriously?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It feels much easier to just use the &lt;strong&gt;&lt;code&gt;useState()&lt;/code&gt;&lt;/strong&gt; hook to manage everything. When your project is small, like a basic counter or even a little e-commerce website, &lt;strong&gt;&lt;code&gt;useState()&lt;/code&gt;&lt;/strong&gt; is beautiful! Life is simple and happy with hooks.&lt;/p&gt;

&lt;p&gt;But the trouble starts when your project gets big. Suddenly, managing dozens of pieces of data and sending information from one deeply nested component to another becomes a huge headache. When one state needs to change in one component, and a different component far away needs that new value, it's not easy to handle that in a manageable and clear way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This huge communication and state-sharing problem is exactly why a powerful tool like Redux was created.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prop Drilling Nightmares: When &lt;strong&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/strong&gt; Becomes Your Enemy?
&lt;/h2&gt;

&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%2Flqhjick33m03axtqcxr3.png" 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%2Flqhjick33m03axtqcxr3.png" alt="Anatomy of basic state managaement" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Scenario:&lt;/strong&gt;&lt;br&gt;
Look at the diagram. This shows how our app's parts, called components, are connected.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;&lt;code&gt;Root&lt;/code&gt;&lt;/strong&gt; is the main component at the top.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The app splits into three main sections: &lt;strong&gt;&lt;code&gt;Navbar&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;Cmp-2&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;Cmp-3&lt;/code&gt;&lt;/strong&gt;.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We have a special piece of information called the user state (like if a user is logged in).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The user state is created and set &lt;em&gt;&lt;code&gt;(i.e., given its value, user info, like logged in)&lt;/code&gt;&lt;/em&gt; on the &lt;strong&gt;&lt;code&gt;Signin&lt;/code&gt;&lt;/strong&gt; component. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;However, our &lt;strong&gt;&lt;code&gt;Navbar&lt;/code&gt;&lt;/strong&gt; also needs this user state so it can change—for example, showing "&lt;strong&gt;&lt;em&gt;&lt;code&gt;Login&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;" or "&lt;strong&gt;&lt;em&gt;&lt;code&gt;Hello, User Name.&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;"&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Problem:&lt;/strong&gt;&lt;br&gt;
The crucial &lt;strong&gt;user state&lt;/strong&gt; is only available far down the component tree, at the &lt;strong&gt;&lt;code&gt;Signin&lt;/code&gt;&lt;/strong&gt; component's level.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;Navbar&lt;/code&gt;&lt;/strong&gt; component, which is near the top, needs this state to work correctly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Old Solution: Prop Drilling:&lt;/strong&gt;&lt;br&gt;
To fix this, the usual way is to move the &lt;strong&gt;user state&lt;/strong&gt; to the very top file, the &lt;strong&gt;&lt;code&gt;Root&lt;/code&gt;&lt;/strong&gt; component.&lt;/p&gt;

&lt;p&gt;Then, we would have to send this &lt;strong&gt;state&lt;/strong&gt; and the &lt;strong&gt;function&lt;/strong&gt; to change it &lt;em&gt;&lt;code&gt;(state function)&lt;/code&gt;&lt;/em&gt; down, one by one, through every child component until it reaches the ones that need it. This is called "&lt;strong&gt;prop drilling&lt;/strong&gt;."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prop drilling&lt;/strong&gt; is a hassle! It makes code hard to read and manage, especially when components in the middle don't even use the state but are just passing it along.&lt;/p&gt;

&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%2F4amc66qqufzrj5kics90.png" 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%2F4amc66qqufzrj5kics90.png" alt="Prop Drilling" width="800" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This illustrates &lt;strong&gt;prop drilling&lt;/strong&gt;, where the &lt;strong&gt;user state&lt;/strong&gt; is manually passed down through every component &lt;em&gt;&lt;code&gt;(Root → Cmp 1 → Cmp 2, etc.)&lt;/code&gt;&lt;/em&gt;. This is a poor approach for large applications because managing &lt;strong&gt;state&lt;/strong&gt; this way becomes overly complex and messy, forcing intermediate components to handle data they don't need.&lt;/p&gt;
&lt;h2&gt;
  
  
  Taming Complexity: A Beginner's Guide to Understanding Redux
&lt;/h2&gt;

&lt;p&gt;According to its official documentation, Redux is a &lt;strong&gt;&lt;code&gt;JavaScript&lt;/code&gt;&lt;/strong&gt; library designed for &lt;strong&gt;"predictable and maintainable global state management."&lt;/strong&gt; This definition simply means that Redux handles your application's data, or state, on a &lt;strong&gt;global level&lt;/strong&gt;. By keeping the state outside of any single component, we can easily access and use this information from &lt;strong&gt;anywhere in the entire application&lt;/strong&gt; whenever it's needed, completely solving the &lt;strong&gt;"prop drilling"&lt;/strong&gt; problem.&lt;/p&gt;
&lt;h2&gt;
  
  
  Under the Hood: How Redux Manages Your Global State
&lt;/h2&gt;

&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%2F0eqccir3nosgvmla55zl.png" 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%2F0eqccir3nosgvmla55zl.png" alt="Redux state management" width="800" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The core of Redux is the &lt;strong&gt;Store&lt;/strong&gt;. Think of the Store as a &lt;strong&gt;single&lt;/strong&gt;, &lt;strong&gt;central container&lt;/strong&gt; that holds all of your application's state data. We use a component called a &lt;strong&gt;&lt;code&gt;Provider&lt;/code&gt;&lt;/strong&gt; to wrap our entire application around this Store. By doing this, we instantly gain &lt;strong&gt;global access&lt;/strong&gt; to all the states held inside. This allows any component, anywhere in the application, to use the necessary state data without any &lt;strong&gt;prop drilling&lt;/strong&gt; whatsoever.&lt;/p&gt;
&lt;h2&gt;
  
  
  Following the State: The Predictable Journey of Data in Redux
&lt;/h2&gt;

&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%2Fr35t94hc8ygubzloikjd.png" 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%2Fr35t94hc8ygubzloikjd.png" alt="Redux workflow anatomy" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;View/UI Action:&lt;/strong&gt; The user interacts with the &lt;strong&gt;&lt;code&gt;View / UI&lt;/code&gt;&lt;/strong&gt; &lt;em&gt;&lt;code&gt;(e.g., clicks a button)&lt;/code&gt;&lt;/em&gt;. The View doesn't change the data directly; instead, it tells the system it wants a change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Action Creation &amp;amp; Dispatch:&lt;/strong&gt; The Vie sends a request to the &lt;strong&gt;Actions creator&lt;/strong&gt;. This creator formats the request into an Action &lt;em&gt;&lt;code&gt;(a plain JavaScript object describing what happened)&lt;/code&gt;&lt;/em&gt;. The Action is then sent forward using &lt;strong&gt;&lt;code&gt;dispatch(action)&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Store Interaction:&lt;/strong&gt; The &lt;strong&gt;&lt;code&gt;dispatched Action&lt;/code&gt;&lt;/strong&gt; is immediately sent to the &lt;strong&gt;Store&lt;/strong&gt;. The Store holds the application's &lt;strong&gt;central state&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;State Update (Reducers):&lt;/strong&gt; The Store forwards the Action &lt;em&gt;&lt;code&gt;(along with the Previous State)&lt;/code&gt;&lt;/em&gt; to the &lt;strong&gt;Reducers&lt;/strong&gt;. The Reducers are &lt;strong&gt;&lt;em&gt;&lt;code&gt;pure functions&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; that take the &lt;strong&gt;&lt;code&gt;old state&lt;/code&gt;&lt;/strong&gt; and the &lt;strong&gt;&lt;code&gt;action&lt;/code&gt;&lt;/strong&gt;, process them, and return a new state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;View Update:&lt;/strong&gt; The Store saves this new state. It then notifies all connected components of the View / UI that the &lt;strong&gt;Current State&lt;/strong&gt; has changed. The &lt;strong&gt;View&lt;/strong&gt; automatically &lt;strong&gt;&lt;em&gt;&lt;code&gt;re-renders&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; with the fresh data, completing the cycle.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Beyond Prop Drilling: Implementing Redux in Your React Workflow
&lt;/h2&gt;

&lt;p&gt;We've finished our theoretical discussion on Redux. Now, let's explore &lt;strong&gt;how to implement Redux within our React project.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There's an excellent tool for this called &lt;strong&gt;&lt;code&gt;React Redux-Toolkit&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;Redux-Toolkit&lt;/code&gt;&lt;/strong&gt; makes our work much easier. It's essentially a package that helps us write &lt;strong&gt;&lt;code&gt;Redux logic&lt;/code&gt;&lt;/strong&gt; in a highly efficient and standardized way.&lt;/p&gt;
&lt;h2&gt;
  
  
  No More Boilerplate: The 3 Core Issues Fixed by Redux-Toolkit
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Configuring a Redux store is too complicated&lt;/li&gt;
&lt;li&gt;You have to add a lot of packages to get Redux to do anything useful&lt;/li&gt;
&lt;li&gt;Redux requires too much boilerplate code&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Redux Integration: Getting Started with Redux in React
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Installation:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;yarn:&lt;/strong&gt; &lt;code&gt;yarn add @reduxjs/toolkit react-redux&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;npm:&lt;/strong&gt; &lt;code&gt;npm i @reduxjs/toolkit react-redux&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Configure Store:
&lt;/h3&gt;

&lt;p&gt;Create a file named &lt;strong&gt;&lt;code&gt;src/app/store.js&lt;/code&gt;&lt;/strong&gt;. Import the &lt;strong&gt;&lt;code&gt;configureStore&lt;/code&gt;&lt;/strong&gt; API from Redux Toolkit. We'll start by creating an empty Redux store, and exporting it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from "@reduxjs/toolkit";

export const store = configureStore({
  reducer: {},
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Provide the Redux Store to React
&lt;/h3&gt;

&lt;p&gt;To make the Redux &lt;strong&gt;&lt;code&gt;store&lt;/code&gt;&lt;/strong&gt; available everywhere in our app, we wrap the entire application with the &lt;strong&gt;&lt;code&gt;&amp;lt;Provider&amp;gt;&lt;/code&gt;&lt;/strong&gt; component from React-Redux. In the &lt;strong&gt;&lt;code&gt;src/index.js&lt;/code&gt;&lt;/strong&gt; file, we simply import the store we created, wrap the main &lt;strong&gt;&lt;code&gt;&amp;lt;RouterProvider router={routes}/&amp;gt;&lt;/code&gt;&lt;/strong&gt; &lt;code&gt;(Because I use react-router-dom)&lt;/code&gt; component with the , and pass the imported store to it using the &lt;strong&gt;&lt;code&gt;store&lt;/code&gt;&lt;/strong&gt; prop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import "./index.css";
import { RouterProvider } from "react-router-dom";
import routes from "./router/routes.jsx";
import { Provider } from "react-redux";
import { store } from "./RTK/store/store.js";

createRoot(document.getElementById("root")).render(
  &amp;lt;StrictMode&amp;gt;
    &amp;lt;Provider store={store}&amp;gt;
      &amp;lt;RouterProvider router={routes} /&amp;gt;
    &amp;lt;/Provider&amp;gt;
  &amp;lt;/StrictMode&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create Redux State Slice
&lt;/h3&gt;

&lt;p&gt;First, create a new file called &lt;strong&gt;&lt;code&gt;src/RTK/features/counter/counterSlice.js&lt;/code&gt;&lt;/strong&gt;. Inside this file, we'll use the &lt;strong&gt;&lt;code&gt;createSlice&lt;/code&gt;&lt;/strong&gt; API from Redux Toolkit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To create a slice, we need three things:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A string name (to identify this part of the state).&lt;/li&gt;
&lt;li&gt;An initial state value.&lt;/li&gt;
&lt;li&gt;Reducer functions that define how the state can be updated.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once the slice is created, we can &lt;strong&gt;&lt;code&gt;export&lt;/code&gt;&lt;/strong&gt; the action creators and the main reducer function for use throughout our app.&lt;/p&gt;

&lt;p&gt;A great feature of Redux Toolkit is that it uses a library called &lt;strong&gt;&lt;code&gt;Immer&lt;/code&gt;&lt;/strong&gt;. While Redux normally requires you to write &lt;strong&gt;&lt;code&gt;immutable&lt;/code&gt;&lt;/strong&gt; updates (meaning you must copy data before changing it), Immer lets you write simpler, "mutating" logic. Redux Toolkit then automatically translates that simple code into the correct immutable updates behind the scenes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createSlice } from "@reduxjs/toolkit";

const initialState = {
  value: 0,
};

export const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    increment: (state) =&amp;gt; {
      state.value += 1;
    },
    decrement: (state) =&amp;gt; {
      state.value -= 1;
    },
    incrementByAmount: (state, action) =&amp;gt; {
      state.value += action.payload;
    },
  },
});

export const { increment, decrement, incrementByAmount } = counterSlice.actions;

export default counterSlice.reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add Slice Reducers to the Store
&lt;/h3&gt;

&lt;p&gt;The next essential step is integrating this new &lt;strong&gt;&lt;code&gt;counterSlice&lt;/code&gt;&lt;/strong&gt; into our main Redux store. We import the reducer function we exported from the slice and add it to the store's reducer parameter. By defining a field for it here &lt;code&gt;(e.g., counter: counterReducer)&lt;/code&gt;, we are telling Redux: "Use this specific reducer function to manage and handle all updates to the state data under the name 'counter'."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from "@reduxjs/toolkit";
import counterSlice from "../features/counter/counterSlice";

export const store = configureStore({
  reducer: {
    counter: counterSlice,
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use Redux State and Actions in React Components
&lt;/h3&gt;

&lt;p&gt;Now that the store is set up, we use React-Redux Hooks to let our components talk to it. The &lt;strong&gt;&lt;code&gt;useSelector&lt;/code&gt;&lt;/strong&gt; hook allows us to read data directly from the store, and the &lt;strong&gt;&lt;code&gt;useDispatch&lt;/code&gt;&lt;/strong&gt; hook gives us the ability to send actions to update that data. To demonstrate, we'll create a new component file, &lt;strong&gt;&lt;code&gt;src/RTK/features/counter/Counter.js&lt;/code&gt;&lt;/strong&gt;, put a &lt;strong&gt;&lt;code&gt;&amp;lt;Counter&amp;gt;&lt;/code&gt;&lt;/strong&gt; component inside it, and finally, import and render that component within our main file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const count = useSelector((state) =&amp;gt; state.counter.value)&lt;br&gt;
  const dispatch = useDispatch()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&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 from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { decrement, increment } from './counterSlice'

export function Counter() {
  const count = useSelector((state) =&amp;gt; state.counter.value)
  const dispatch = useDispatch()

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button
          aria-label="Increment value"
          onClick={() =&amp;gt; dispatch(increment())}
        &amp;gt;
          Increment
        &amp;lt;/button&amp;gt;
        &amp;lt;span&amp;gt;{count}&amp;lt;/span&amp;gt;
        &amp;lt;button
          aria-label="Decrement value"
          onClick={() =&amp;gt; dispatch(decrement())}
        &amp;gt;
          Decrement
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion: Goodbye, Prop Drilling!
&lt;/h2&gt;

&lt;p&gt;You've now successfully implemented the core of Redux in your React application using the Redux Toolkit. By setting up a centralized Store and connecting it with the &lt;strong&gt;&lt;code&gt;&amp;lt;Provider&amp;gt;&lt;/code&gt;&lt;/strong&gt;, you have eliminated the messy problem of prop drilling. Now, any component can use &lt;strong&gt;&lt;code&gt;useSelector&lt;/code&gt;&lt;/strong&gt; to read state and &lt;strong&gt;&lt;code&gt;useDispatch&lt;/code&gt;&lt;/strong&gt; to update state, making your application significantly more predictable and easier to maintain. This scalable structure is key to building large, professional applications.&lt;/p&gt;

&lt;h4&gt;
  
  
  What's Next? Handling Asynchronous Data
&lt;/h4&gt;

&lt;p&gt;So far, we've only handled local state updates. But what about fetching data from a server or calling an &lt;strong&gt;API&lt;/strong&gt;? In the next article, we'll dive into how Redux handles these crucial asynchronous tasks using a powerful tool within the Toolkit: &lt;strong&gt;&lt;code&gt;createAsyncThunk&lt;/code&gt;&lt;/strong&gt;. Stay tuned to learn how to manage complex API calls and loading states the Redux way!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>redux</category>
      <category>reduxtoolkit</category>
    </item>
    <item>
      <title>Stop the Mess: Structuring Your First Node.js Application</title>
      <dc:creator>Saleh Ahmed Mahin</dc:creator>
      <pubDate>Sun, 05 Oct 2025 18:42:24 +0000</pubDate>
      <link>https://forem.com/salehahmedmahin/stop-the-mess-structuring-your-first-nodejs-application-3131</link>
      <guid>https://forem.com/salehahmedmahin/stop-the-mess-structuring-your-first-nodejs-application-3131</guid>
      <description>&lt;p&gt;&lt;strong&gt;Today, I'm going to talk about a folder structure that is specifically designed for beginners.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you first start making a &lt;strong&gt;&lt;code&gt;Node.js&lt;/code&gt;&lt;/strong&gt; app, you might put all your code into just one file, maybe called &lt;strong&gt;&lt;code&gt;index.js&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;app.js&lt;/code&gt;&lt;/strong&gt;. It's like putting everything in your kitchen - the food, the pots, the cleaning supplies - all into one big box! That works at the beginning, but soon your file gets really long and messy, making it hard to find things, fix mistakes, or add new features later on.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&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%2Fkdg2ktjh0ts0q49x56jp.png" 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%2Fkdg2ktjh0ts0q49x56jp.png" alt="A folder structure of basic project" width="195" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What's the issues?
&lt;/h2&gt;

&lt;p&gt;Every beginner starts with the basic files like &lt;strong&gt;&lt;code&gt;index.js&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt;. This is perfect for a simple app or when you are just learning. As the project grows larger, keeping all your code in one file becomes a massive headache, making the entire application complex and hard to manage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which type of complexity?
&lt;/h2&gt;

&lt;p&gt;On a large project, you write a huge amount of &lt;strong&gt;&lt;code&gt;business logic&lt;/code&gt;&lt;/strong&gt;, create many &lt;strong&gt;&lt;code&gt;APIs&lt;/code&gt;&lt;/strong&gt;, and manage all the &lt;strong&gt;&lt;code&gt;database&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;schema details&lt;/code&gt;&lt;/strong&gt;. Putting all that code into one place makes it a complex, unreadable mess, which quickly creates major maintenance issues and makes it almost impossible for new team members to jump in.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to fix this complexity?
&lt;/h2&gt;

&lt;p&gt;To make things easier as your app grows, you should start putting different parts of the code into their own separate files. For example, your &lt;strong&gt;&lt;code&gt;api routes&lt;/code&gt;&lt;/strong&gt; go in one folder, your all &lt;strong&gt;&lt;code&gt;business logic&lt;/code&gt;&lt;/strong&gt; &lt;em&gt;&lt;code&gt;(controllers)&lt;/code&gt;&lt;/em&gt; in another and so on. This keeps your code neat and organized, making your life much easier in the long run.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I Organized My Node.js App: A Step-by-Step Folder Structure Guide
&lt;/h2&gt;

&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%2Fjzbkcfibqybrflz8if1d.png" 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%2Fjzbkcfibqybrflz8if1d.png" alt="Organized folder structure" width="186" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I use this specific folder structure for my basic projects, such as a simple e-commerce application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's breakdown it:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;Authorization:&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&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%2Fnf2ezyhov0h5bjf1r6es.png" 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%2Fnf2ezyhov0h5bjf1r6es.png" alt="Authorization" width="169" height="23"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I primarily use the &lt;strong&gt;&lt;code&gt;authorization&lt;/code&gt;&lt;/strong&gt; folder to handle all my &lt;strong&gt;&lt;em&gt;&lt;code&gt;authentication&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;&lt;code&gt;authorization-related API logic&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;, such as tasks like creating &lt;strong&gt;&lt;code&gt;JWTs&lt;/code&gt;&lt;/strong&gt; &lt;em&gt;&lt;code&gt;(JSON Web Tokens)&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;DB:&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&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%2Fhunfeectr3m28lod9tfu.png" 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%2Fhunfeectr3m28lod9tfu.png" alt="Database" width="87" height="25"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;DB&lt;/code&gt;&lt;/strong&gt; folder is where I put all the code for connecting to the &lt;em&gt;&lt;strong&gt;&lt;code&gt;database&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt; and creating the necessary &lt;strong&gt;&lt;em&gt;&lt;code&gt;data collections&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;Middleware:&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&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%2Fjv3126yczqsnhoa01h8j.png" 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%2Fjv3126yczqsnhoa01h8j.png" alt="Middleware" width="153" height="25"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I mainly use the &lt;strong&gt;&lt;code&gt;middleware&lt;/code&gt;&lt;/strong&gt; folder to create all the &lt;strong&gt;&lt;em&gt;&lt;code&gt;middleware functions&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; for the project. For example, after creating a &lt;strong&gt;&lt;em&gt;&lt;code&gt;JWT token&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;, you also need to verify it. For that, I create a &lt;strong&gt;&lt;em&gt;&lt;code&gt;middleware&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; named &lt;strong&gt;&lt;em&gt;&lt;code&gt;verifyJWT&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;, which I can then import and use anywhere else in the project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;routerController:&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&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%2Fke3bcqv4hzi3fs9y7d7a.png" 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%2Fke3bcqv4hzi3fs9y7d7a.png" alt="Controllers" width="182" height="25"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;routerController&lt;/code&gt;&lt;/strong&gt; folder's name basically tells you its job: it holds the business logic for your &lt;strong&gt;&lt;em&gt;&lt;code&gt;API's routes&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this project, I put both the &lt;strong&gt;&lt;em&gt;&lt;code&gt;route definitions&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;&lt;code&gt;(the actual endpoints)&lt;/code&gt;&lt;/em&gt; and their &lt;strong&gt;&lt;em&gt;&lt;code&gt;business logic&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;&lt;code&gt;(what the API does)&lt;/code&gt;&lt;/em&gt; inside this one &lt;strong&gt;&lt;code&gt;routerController&lt;/code&gt;&lt;/strong&gt; folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But A Better Practice (For Larger Apps):&lt;/strong&gt;&lt;br&gt;
A better practice is to separate those two jobs completely.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a separate &lt;strong&gt;&lt;code&gt;routes&lt;/code&gt;&lt;/strong&gt; folder where you only define the &lt;strong&gt;&lt;em&gt;&lt;code&gt;paths endpoint&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; (like &lt;code&gt;/users&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keep your &lt;strong&gt;&lt;em&gt;&lt;code&gt;business logic&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; separate in a &lt;strong&gt;&lt;code&gt;controllers&lt;/code&gt;&lt;/strong&gt; folder.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;Index.js&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&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%2Fc2ky6izzv76fkei8f0jy.png" 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%2Fc2ky6izzv76fkei8f0jy.png" alt="index.js file" width="105" height="27"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally, we have the &lt;strong&gt;&lt;code&gt;index.js&lt;/code&gt;&lt;/strong&gt; file. This is considered the &lt;strong&gt;&lt;code&gt;main entry file&lt;/code&gt;&lt;/strong&gt; for the whole server - it's the first file that runs when you start your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time to Say Goodbye to the Messy index.js!
&lt;/h2&gt;

&lt;p&gt;Moving away from one huge &lt;strong&gt;&lt;code&gt;index.js&lt;/code&gt;&lt;/strong&gt; file is the best way to start building serious apps. By splitting your code into organized folders, you make the project easy to understand and much simpler to grow later on. It saves you tons of headaches!&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
