Code With Riad
๐จโ๐ป Riad Mahamud | MERN Stack Developer
๐ Sharing code tips, dev projects & modern web solutions.
06/11/2025
โก Day 37 of my React.js Journey
Todayโs topic: Building a Custom Hook โ useDebounce
When a user types fast (like in a search bar), we donโt want to call the API on every keystroke.
๐ We โdebounceโ the input โ wait for the user to stop typing before running the logic.
Letโs build it! ๐
import { useState, useEffect } from "react";
function useDebounce(value, delay = 500) {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => setDebouncedValue(value), delay);
return () => clearTimeout(handler);
}, [value, delay]);
return debouncedValue;
}
// Usage
function Search() {
const [query, setQuery] = useState("");
const debouncedQuery = useDebounce(query, 600);
useEffect(() => {
if (debouncedQuery) console.log("Search for:", debouncedQuery);
}, [debouncedQuery]);
return setQuery(e.target.value)} placeholder="Search..." />;
}
โ
Why useDebounce matters:
Prevents unnecessary API calls
Boosts performance
Creates a smoother user experience
๐ก Used in search bars, live filters, or form validations!
Tomorrow: Building a Custom Hook โ usePrevious (track previous values) ๐
25/10/2025
๐งฉ Day 35 of my React.js Journey
Todayโs topic: Custom Hooks โ Reusing Logic Like a Pro
React Hooks like useState and useEffect are greatโฆ but what if you need the same logic in multiple components?
๐ Thatโs where Custom Hooks come in.
They let you extract and reuse stateful logic cleanly.
Example:
import { useState, useEffect } from "react";
function useFetch(url) {
const [data, setData] = useState(null);
useEffect(() => {
fetch(url)
.then(res => res.json())
.then(setData);
}, [url]);
return data;
}
// Usage
function Users() {
const users = useFetch("https://jsonplaceholder.typicode.com/users");
return {JSON.stringify(users, null, 2)};
}
โ
Why use Custom Hooks:
Reuse logic without repeating code
Keep components clean
Share easily across projects
๐ก Naming rule: always start with use (e.g., useAuth, useForm, useDarkMode)
Tomorrow: Building a Custom Hook โ useLocalStorage ๐ง
08/10/2025
๐ Day 34 of my React.js Journey
Todayโs topic: Advanced Pattern โ React.lazy + Suspense for Dynamic Imports
When your app grows, you donโt want to load every component at once.
๐ Thatโs where code-splitting comes in using React.lazy() and Suspense.
It lets React load components only when needed, improving performance and reducing bundle size.
Example:
import React, { Suspense } from "react";
const Profile = React.lazy(() => import("./Profile"));
function App() {
return (
);
}
โ
How it helps:
Loads big components on demand
Boosts initial load speed
Perfect for routes, modals, and dashboard sections
๐ก Pro tip: Combine with React Router to lazy-load entire pages!
Tomorrow: Custom Hooks โ Reusing Logic Like a Pro ๐ง
06/10/2025
โ๏ธ Day 33 of my React.js Journey
Todayโs topic: Performance Optimization with React.memo & useCallback
As apps grow, unnecessary re-renders can slow things down. React gives us two great tools to fix that:
๐ง React.memo
Prevents a component from re-rendering unless its props change.
const Child = React.memo(({ value }) => {
console.log("Rendered");
return {value};
});
โก useCallback
Prevents a function from being recreated on every render.
function Parent() {
const [count, setCount] = useState(0);
const handleClick = useCallback(() => setCount(c => c + 1), []);
return ;
}
โ
Why this matters:
Keeps your app fast
Reduces wasted re-renders
Crucial for lists, forms, and heavy UIs
๐ก Pro tip: Use them when needed โ overusing can make debugging harder.
Tomorrow: React.lazy + Suspense for Dynamic Imports (Advanced Pattern) ๐
03/10/2025
โ
Day 32 of my React.js Journey
Todayโs topic: Todo App with Context + useReducer
Yesterday we learned useReducer. Today, letโs combine it with Context API to manage global state-perfect for a Todo App.
Example:
import React, { createContext, useReducer, useContext } from "react";
const TodoContext = createContext();
function todoReducer(state, action) {
switch (action.type) {
case "add":
return [...state, { id: Date.now(), text: action.text }];
case "remove":
return state.filter(todo => todo.id !== action.id);
default:
return state;
}
}
function TodoProvider({ children }) {
const [todos, dispatch] = useReducer(todoReducer, []);
return (
{children}
);
}
function TodoList() {
const { todos, dispatch } = useContext(TodoContext);
return (
dispatch({ type: "add", text: "New Task" })}>
Add Todo
{todos.map(todo => (
{todo.text}
dispatch({ type: "remove", id: todo.id })}>
โ
))}
);
}
export default function App() {
return (
);
}
๐ With this approach:
Context provides global state
Reducer manages complex updates
Any component can add/remove todos
Tomorrow: React.memo & useCallback โ optimize performance.
02/10/2025
๐ Day 31 of my React.js Journey
Todayโs topic: useReducer Hook
When state management gets complex (multiple values, deep updates), useState can become messy.
๐ Thatโs where useReducer shines.
Itโs like a mini Redux built into React.
Example:
import React, { useReducer } from "react";
function reducer(state, action) {
switch (action.type) {
case "increment":
return { count: state.count + 1 };
case "decrement":
return { count: state.count - 1 };
default:
return state;
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
return (
Count: {state.count}
dispatch({ type: "increment" })}>+
dispatch({ type: "decrement" })}>-
);
}
โ
Why use useReducer?
Keeps logic organized in one reducer function
Easy to scale for complex state
Pairs perfectly with Context API for global state
Tomorrow: Building a Todo App with Context + Reducer ๐ ๏ธ
01/10/2025
๐ฏ Day 30 of my React.js Journey โ Milestone Post ๐
Todayโs topic: Context API Deep Dive
React apps often suffer from โprop drillingโ โ passing props down multiple levels.
๐ The Context API solves this by sharing data across components without manually passing props.
Example:
import React, { createContext, useContext, useState } from "react";
const ThemeContext = createContext();
function App() {
const [theme, setTheme] = useState("light");
return (
);
}
function Toolbar() {
const { theme, setTheme } = useContext(ThemeContext);
return (
setTheme(theme === "light" ? "dark" : "light")}>
Current Theme: {theme}
);
}
โ
Benefits of Context:
Share state globally without prop drilling
Great for themes, authentication, language settings, etc.
Cleaner & more maintainable code
โ ๏ธ Use carefully: too much context can make debugging harder โ consider Redux, Zustand, or Jotai for larger apps.
๐ 30 Days Recap:
โ๏ธ Components & Props
โ๏ธ Hooks (useState, useEffect, useContext, etc.)
โ๏ธ React Router basics
โ๏ธ Performance (lazy loading, error boundaries, portals)
Next chapter โ Advanced Patterns & State Management ๐ฅ
29/09/2025
๐ช Day 29 of my React.js Journey
Todayโs topic: React Portals
By default, React renders components inside the root DOM node. But sometimes, we need to render outside of it (like modals, tooltips, or dropdowns).
๐ Thatโs where Portals come in.
Example:
import ReactDOM from "react-dom";
function Modal({ children }) {
return ReactDOM.createPortal(
{children},
document.getElementById("modal-root")
);
}
โ
Benefits of Portals:
Keep components logically in React tree but render outside DOM hierarchy
Solve z-index issues with modals/popups
Improve accessibility (focus trapping, screen readers)
So next time your modal breaks layout โ use Portals instead of hacks. ๐
Tomorrow: Context API Deep Dive โ when and how to use it.
28/09/2025
๐ก๏ธ Day 28 of my React.js Journey
Todayโs topic: Error Boundaries in React
Even the best React apps can crash because of runtime errors. To avoid breaking the entire UI, React provides Error Boundaries.
๐ Error Boundaries are React components that catch errors in their child components and display a fallback UI instead.
Example:
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError() {
return { hasError: true };
}
componentDidCatch(error, info) {
console.error("Error caught:", error, info);
}
render() {
if (this.state.hasError) {
return Something went wrong!;
}
return this.props.children;
}
}
Usage:
โ
Benefits:
Prevents full app crashes
Shows graceful fallback UI
Helps with debugging in production
โ ๏ธ Note: Error Boundaries do not catch errors in event handlers, async code, or server-side rendering.
Tomorrow: React Portals โ rendering outside the root div.
๐ New JavaScript Project Completed โ Sprite Animation with Multiple Effects! ๐จโจ
I recently built a simple sprite animation using JavaScript where a small plant comes to life ๐ฑ with different animation effects:
โ
Grow โ Smooth scaling animation
โ
Wink โ Playful flicker effect
โ
Float โ Gentle floating motion
โ
Hide โ Seamless fade-out transition
โ
All Animations โ Combined effects for extra fun!
This project was a great way to practice DOM manipulation, CSS transitions, and JavaScript event handling. ๐ฏ
It also shows how powerful even simple animations can be in creating interactive and engaging user experiences.
๐ก Whether itโs a small web app, a game, or a playful UI component, animations help improve UX and keep users engaged.
๐ If youโre passionate about creative coding or want to make your websites more interactive and fun, letโs connect!
Click here to claim your Sponsored Listing.
Category
Website
Address
5000