𝐓𝐨𝐝𝐚𝐲 𝐈 𝐥𝐞𝐯𝐞𝐥𝐞𝐝 𝐮𝐩 𝐦𝐲 𝐑𝐞𝐚𝐜𝐭 𝐬𝐤𝐢𝐥𝐥𝐬 𝐛𝐲 𝐢𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭𝐢𝐧𝐠 𝐬𝐦𝐨𝐨𝐭𝐡, 𝐟𝐥𝐢𝐜𝐤𝐞𝐫-𝐟𝐫𝐞𝐞 𝐩𝐚𝐠𝐢𝐧𝐚𝐭𝐢𝐨𝐧 𝐮𝐬𝐢𝐧𝐠 𝐑𝐞𝐚𝐜𝐭 𝐐𝐮𝐞𝐫𝐲! I was exploring how to load API data page-by-page without the UI blinking or resetting and React Query’s 𝒌𝒆𝒆𝒑𝑷𝒓𝒆𝒗𝒊𝒐𝒖𝒔𝑫𝒂𝒕𝒂 feature completely changed the game for me. 🔥 What I Built - Dynamic Pagination using useState - Data Fetching with useQuery - Personalized API calls using pageNumber - Smooth transitions using keepPreviousData - Clean, minimal UI for posts + pagination buttons - Proper error & loading handling What I Learned 🔹 React Query refetches only when queryKey changes → ["posts", pageNumber] 🔹 queryFn should always be a function → () => FetchPosts(pageNumber) 🔹 API must receive the correct page number to return limited results 🔹 keepPreviousData helps avoid UI jumps and blank screens 🔹 Good CSS structure matters — it prevents overlapping and maintains layout 🎯 Outcome A smooth, modern, user-friendly pagination UI where: ✨ No flickering ✨ No layout shifting ✨ Old data stays visible while new data loads ✨ Perfect experience for users 🛠️ 𝑻𝒆𝒄𝒉 𝑺𝒕𝒂𝒄𝒌 : React.js, React Query, Axios, JSONPlaceholder API, Custom CSS I’m really enjoying diving deeper into React Query — the amount of control it gives over data fetching is incredible! Excited to keep building and learning more every day! #react #reactjs #reactquery #webdevelopment #frontenddeveloper #learningjourney #javascript
More Relevant Posts
-
𝗙𝗿𝗼𝗺 𝘂𝘀𝗲𝗘𝗳𝗳𝗲𝗰𝘁() 𝘁𝗼 𝘂𝘀𝗲() - 𝗥𝗲𝗮𝗰𝘁 𝟭𝟵 𝗠𝗮𝗸𝗲𝘀 𝗗𝗮𝘁𝗮 𝗙𝗲𝘁𝗰𝗵𝗶𝗻𝗴 𝗙𝗲𝗲𝗹 𝗘𝗳𝗳𝗼𝗿𝘁𝗹𝗲𝘀𝘀 For years, fetching data in React required more setup than it should have. • You wrote useState to hold data. • You added useEffect to trigger a fetch. • You tracked dependencies to avoid infinite loops. • You still got that tiny flicker every render. All that - just to show one API response. React 19 fixes this with one idea: 𝘂𝘀𝗲() use() brings async logic into the render itself. 𝗛𝗼𝘄 𝗶𝘁 𝘄𝗼𝗿𝗸𝘀 (𝘂𝗻𝗱𝗲𝗿 𝘁𝗵𝗲 𝗵𝗼𝗼𝗱): • React sees a Promise inside use() and pauses the render. • It waits for the Promise to resolve without blocking the rest of the app. • Once resolved, React resumes rendering with real data in place. • No re-renders, no flicker, no loading transitions unless you define them. Everything happens within the render phase, keeping UI and data perfectly aligned. 𝗪𝗵𝗮𝘁 𝘁𝗵𝗶𝘀 𝗺𝗲𝗮𝗻𝘀: • No “loading → data” flicker. • No second render after fetching. • No local state just to store results. • No dependency debugging or re-run headaches. 𝗥𝗲𝗳𝗲𝘁𝗰𝗵𝗶𝗻𝗴 𝗺𝗮𝗱𝗲 𝘀𝗶𝗺𝗽𝗹𝗲: • Change the input (like userId) → React cancels the old Promise and runs a new one automatically. • No cleanup. No manual reset. React handles it. • Error handling built in • If the Promise fails, React forwards the error to the nearest ErrorBoundary. No try/catch, no setError. 𝗕𝗲𝗳𝗼𝗿𝗲 𝗥𝗲𝗮𝗰𝘁 𝟭𝟵: • Data fetching happened after rendering (useEffect). • UI updated only after the data arrived. • You managed states, effects, and dependencies manually. 𝗪𝗶𝘁𝗵 𝗥𝗲𝗮𝗰𝘁 𝟭𝟵: • Data fetching happens during rendering. • React pauses and resumes automatically. • UI and data are always in sync. 𝗞𝗲𝘆 𝗯𝗲𝗻𝗲𝗳𝗶𝘁𝘀: • No useEffect for fetching. • No state just to store API results. • No dependency arrays. • No double renders. • Works seamlessly with Suspense and concurrent rendering. React 19 turns asynchronous data into a first-class part of rendering. Less setup. Less code. More predictable Would love to know your thoughts in comments ✨️ #ReactJS #ReactHooks #WebDevelopment #JavaScript #React19 #FrontendDevelopment #Frontend #Javascript
To view or add a comment, sign in
-
-
💡 Frontend Dev Trick: Another way to get what you want — with a bit of flatMap magic! You know those APIs that love to send back data inside data, wrapped in an array, sitting next to a meta or something like that? It’s like opening a gift box just to find another box inside it. 🎁 Something like this: [ { "data": [ { /* your items */ } ], "meta": { /* pagination info */ } } ] Most of us just go: const items = response[0].data; And hey — that works! 👏 No one’s judging. But sometimes, when you’re juggling multiple pages of results or working with React Query’s useInfiniteQuery, there’s a smoother move. Enter our elegant little friend: flatMap. ⚛️ When React Query Meets flatMap When you use useInfiniteQuery, your data usually looks like a stack of pages. Here’s the classic way: const posts = data.pages.map(page => page.data).flat(); Now, with flatMap, it’s like telling JavaScript: “Yeah, do both things at once, buddy.” const posts = data.pages.flatMap(page => page.data); One line. Elegant. Easy to read. Less typing — more sipping your coffee ☕. 💬 When do I use it? Whenever I’m transforming paginated API responses or reshaping data to fit existing component props — especially with infinite scrolls. It’s not the only way, but it’s a neat one-liner that keeps your code looking sharp. So next time you’re wrangling paginated data, maybe give flatMap a try — not because you have to, but because it feels… satisfying. 😎 What about you? Got your own tricks for flattening or shaping API responses? Drop them below — let’s trade a few elegant hacks! 💬 #frontend #reactjs #reactquery #javascript #webdev #cleancode #developerlife #codingtips Just dont forget about optional chaining bro... 😅
To view or add a comment, sign in
-
-
𝗔𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗰 𝗦𝗲𝗺𝗶𝗰𝗼𝗹𝗼𝗻 𝗜𝗻𝘀𝗲𝗿𝘁𝗶𝗼𝗻 (𝗔𝗦𝗜) 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 --> 𝘁𝗵𝗲 “𝗵𝗲𝗹𝗽𝗳𝘂𝗹” 𝗳𝗲𝗮𝘁𝘂𝗿𝗲 𝘁𝗵𝗮𝘁 𝘀𝗼𝗺𝗲𝘁𝗶𝗺𝗲𝘀 𝗵𝗲𝗹𝗽𝘀 𝗮 𝗹𝗶𝘁𝘁𝗹𝗲 𝘁𝗼𝗼 𝗺𝘂𝗰𝗵. JavaScript automatically inserts semicolons (;) when it thinks you forgot them. Sounds convenient. Well... sometimes it can change your code’s meaning entirely! 𝗛𝗲𝗿𝗲’𝘀 𝗮 𝗰𝗹𝗮𝘀𝘀𝗶𝗰 𝗲𝘅𝗮𝗺𝗽𝗹𝗲 👇 function getData() { return { message: "Hello World" } } console.log(getData()); You’d expect it to log { message: "Hello World" } 𝗕𝘂𝘁 𝘀𝘂𝗿𝗽𝗿𝗶𝘀𝗲 --> It logs undefined 𝗪𝗵𝘆? Because ASI inserts a semicolon right after return return; // inserted automatically { message: "Hello World" } So the function actually returns nothing. 𝗧𝗼 𝗳𝗶𝘅 𝗶𝘁: function getData() { return { message: "Hello World" } } 𝗧𝗶𝗽: Always keep the return and the value on the same line. ASI is like that friend who “fixes” your code but never tells you what they changed. Always use semicolons intentionally, not accidentally. #JavaScript #WebDevelopment #Frontend #CleanCode #DevCommunity
To view or add a comment, sign in
-
🚀 Next.js Magic: "getStaticProps" vs "getServerSideProps" If you’ve ever wondered when to use which — here’s the breakdown 👇 🧱 "getStaticProps" — Static Site Generation (SSG) - Runs at build time - Generates HTML + JSON once and reuses it for every request - Super fast — because content is served via CDN - Ideal for: ✅ Blogs ✅ Marketing pages ✅ Docs or FAQs 🕓 Optional: Add "revalidate" to enable Incremental Static Regeneration (ISR) — allowing static pages to update after deployment without a full rebuild. export async function getStaticProps() { const data = await fetch('https://lnkd.in/gMYFrdHq'); return { props: { data }, revalidate: 10 }; } --- 🌐 "getServerSideProps" — Server-Side Rendering (SSR) - Runs on every request - HTML is generated on the server, sent to the browser - Slower than SSG but always up-to-date - Ideal for: ✅ Dashboards ✅ Personalized data (user/session-based) ✅ Real-time content export async function getServerSideProps(context) { const data = await fetch('https://lnkd.in/gcyj7Xhk' + context.query.id); return { props: { data } }; } --- 💡 TL;DR - Use "getStaticProps" when data rarely changes (speed > freshness) - Use "getServerSideProps" when data changes frequently (freshness > speed) ⚡️ Next.js gives you the best of both worlds — choose wisely for performance + user experience. #Nextjs #WebDevelopment #Frontend #React #Performance #JavaScript
To view or add a comment, sign in
-
🚀 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗗𝗲𝗲𝗽 𝗗𝗶𝘃𝗲: 𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝘁𝗵𝗲 𝗙𝗲𝘁𝗰𝗵 𝗔𝗣𝗜 💡 Say goodbye to old-school XMLHttpRequest! The Fetch API is the modern, promise-based way to make HTTP requests in JavaScript — simple, powerful, and elegant. ⚡ 🔹 𝗕𝗮𝘀𝗶𝗰 𝗙𝗲𝘁𝗰𝗵 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: fetch('https://lnkd.in/gWKpgMrT') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error)); ✅ fetch() returns a Promise ✅ response.json() converts response to JSON ✅ .catch() ensures error handling 🔹 𝗖𝗹𝗲𝗮𝗻𝗲𝗿 𝗦𝘆𝗻𝘁𝗮𝘅 𝘄𝗶𝘁𝗵 𝗔𝘀𝘆𝗻𝗰/𝗔𝘄𝗮𝗶𝘁: async function getData() { try { const response = await fetch('https://lnkd.in/gWKpgMrT'); const data = await response.json(); console.log(data); } catch (error) { console.error('Error:', error); } } getData(); ✨ No more nested .then() chains — just clean, readable async code! 💡 𝗣𝗿𝗼 𝗧𝗶𝗽𝘀: 🔸 Always handle network errors 🔸 Add headers for authentication & content-type 🔸 Supports GET, POST, PUT, DELETE and more Credit 💳 🫡 Sameer Basha Shaik 🚀 The Fetch API is a must-know tool for every frontend developer — perfect for fetching data, integrating APIs, and building modern web applications! 🌐 #JavaScript #FetchAPI #WebDevelopment #AsyncAwait #Frontend #CodingTips #CleanCode #DeveloperCommunity #Promises #LearnToCode #jsdeveloper
To view or add a comment, sign in
-
⚡ 𝗠𝗮𝘀𝘁𝗲𝗿 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗔𝗿𝗿𝗮𝘆𝘀 — 𝟴 𝗚𝗮𝗺𝗲-𝗖𝗵𝗮𝗻𝗴𝗶𝗻𝗴 𝗠𝗲𝘁𝗵𝗼𝗱𝘀 𝗘𝘃𝗲𝗿𝘆 𝗗𝗲𝘃 𝗦𝗵𝗼𝘂𝗹𝗱 𝗞𝗻𝗼𝘄 🚀 Arrays are the heartbeat of JavaScript. Whether you’re transforming data, filtering responses, or rendering UI — arrays are everywhere. But to write clean, optimized, and professional code, you need to go beyond basic loops. 𝗛𝗲𝗿𝗲 𝗮𝗿𝗲 𝟴 𝗮𝗿𝗿𝗮𝘆 𝗺𝗲𝘁𝗵𝗼𝗱𝘀 𝘁𝗵𝗮𝘁 𝘄𝗶𝗹𝗹 𝘁𝗮𝗸𝗲 𝘆𝗼𝘂𝗿 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝘀𝗸𝗶𝗹𝗹𝘀 𝘁𝗼 𝘁𝗵𝗲 𝗻𝗲𝘅𝘁 𝗹𝗲𝘃𝗲𝗹 👇 1️⃣ 𝗺𝗮𝗽() → Transforms each element and returns a new array. 🔹 Example: Rendering product cards or modifying fetched data. 2️⃣ 𝗳𝗶𝗹𝘁𝗲𝗿() → Returns elements that meet a condition. 🔹 Example: Filtering active users or removing null values. 3️⃣ 𝗿𝗲𝗱𝘂𝗰𝗲() → Reduces an array to a single value. 🔹 Example: Calculating totals, averages, or merging objects. 4️⃣ 𝘀𝗼𝗺𝗲() → Checks if at least one element matches a condition. 🔹 Example: Quick validations like “is any user logged in?”. 5️⃣ 𝗲𝘃𝗲𝗿𝘆() → Ensures all elements meet a condition. 🔹 Example: Confirm all form fields are valid before submission. 6️⃣ 𝗶𝗻𝗰𝗹𝘂𝗱𝗲𝘀() → Checks if an array contains a specific value. 🔹 Example: Searching tags or validating user roles. 7️⃣ 𝗷𝗼𝗶𝗻() → Merges elements into a single string. 🔹 Example: Creating comma-separated lists or formatted output. 8️⃣ 𝘀𝗽𝗹𝗶𝗰𝗲() → Adds or removes elements directly (mutates the array). 🔹 Example: Updating lists or dynamic UI arrays in React. 💡 𝗣𝗿𝗼 𝗧𝗶𝗽: Master these and you’ll handle 90% of real-world data transformations with ease. 💬 𝑊ℎ𝑖𝑐ℎ 𝑎𝑟𝑟𝑎𝑦 𝑚𝑒𝑡ℎ𝑜𝑑 𝑑𝑜 𝑦𝑜𝑢 𝑢𝑠𝑒 𝑡ℎ𝑒 𝑚𝑜𝑠𝑡 𝑖𝑛 𝑦𝑜𝑢𝑟 𝑝𝑟𝑜𝑗𝑒𝑐𝑡𝑠? 𝐷𝑟𝑜𝑝 𝑦𝑜𝑢𝑟 𝑎𝑛𝑠𝑤𝑒𝑟 𝑏𝑒𝑙𝑜𝑤 👇 credit- Ania Eftekhari #JavaScript #WebDevelopment #Frontend
To view or add a comment, sign in
-
React 19 just made one of the biggest quality-of-life upgrades ever: data fetching without useEffect(). If you’ve been building with React for a while, you know the pain: You write useState to store data. You set up useEffect to fetch it. You pray your dependency array doesn’t break something. And then you still get that flicker between “loading” and “loaded.” React 19 changes that completely. Introducing use() — a brand-new hook that brings async fetching directly into the render phase. Here’s what that means: • React now pauses rendering when it encounters a Promise. • It waits for it to resolve without blocking the rest of the UI. • Once data arrives, it resumes rendering with the final content. No flicker. No double render. No manual states or effects. This changes everything about how we fetch data: • No more useEffect just for API calls • No local state to hold results • No dependency debugging • No try/catch — errors automatically flow to the nearest ErrorBoundary React 19’s use() makes async data a first-class part of rendering. Fetching, refetching, and error handling — all handled natively by React itself. Less boilerplate. More predictability. Cleaner UI flow. This is the React we always wanted. I’ve attached a visual breakdown to make it easier to understand. What’s your take? Does use() finally solve React’s biggest headache? #React19 #ReactJS #ReactHooks #WebDevelopment #FrontendDevelopment #JavaScript #Frontend #Coding #DeveloperExperience #ReactTips
To view or add a comment, sign in
-
-
⚛️ That moment when I finally got my API call to work… and data appeared on the screen! 😍 When I first started learning React, I thought fetching data was simple — just call the API and show the data. But then… the re-renders, async calls, and errors started showing up like plot twists 😅 That’s when I learned the right way 👇 ✅ Using useEffect() for API calls: import React, { useEffect, useState } from "react"; function UserList() { const [users, setUsers] = useState([]); useEffect(() => { fetch("https://lnkd.in/gTcasaiP") .then(res => res.json()) .then(data => setUsers(data)) .catch(err => console.error(err)); }, []); return ( <ul> {users.map(user => <li key={user.id}>{user.name}</li>)} </ul> ); } 💡 Lesson learned: Always use useEffect() for API calls to avoid infinite loops. Handle loading and errors gracefully. Keep your data state clean and predictable. Once I understood this pattern, fetching data in React felt like second nature. ⚡ #ReactJS #WebDevelopment #FrontendDeveloper #MERNStack #JavaScript #API #useEffect #ReactHooks #LearningByDoing #CodingJourney
To view or add a comment, sign in
-
𝗪𝗮𝗻𝘁 𝘁𝗼 𝗳𝗲𝘁𝗰𝗵 𝗱𝗮𝘁𝗮 𝘄𝗶𝘁𝗵𝗼𝘂𝘁 𝗯𝗹𝗼𝗰𝗸𝗶𝗻𝗴 𝘆𝗼𝘂𝗿 𝗨𝗜? 𝗧𝗿𝘆 𝗥𝗲𝗮𝗰𝘁 𝗦𝘂𝘀𝗽𝗲𝗻𝘀𝗲! Suspense lets you "wait" for async operations (like data fetching) without blocking the entire UI. No more loading spinners that freeze your app! How it works: → Wrap your async component with 𝗥𝗲𝗮𝗰𝘁.𝗦𝘂𝘀𝗽𝗲𝗻𝘀𝗲 → Use a "fallback" component (e.g., a spinner or skeleton screen) while data is loading → Once data is ready, React automatically swaps in the fully loaded component Benefits: • Improved user experience—only the relevant part of the UI waits for data • Cleaner code—no more manual loading states or conditional rendering • Seamless integration with React Query, SWR, or other data-fetching libraries Example: ``` <Suspense fallback={<Spinner />}> <AsyncComponent /> </Suspense> ``` Tip: Combine Suspense with 𝗥𝗲𝗮𝗰𝘁.𝗹𝗮𝘇𝘆 for even better performance. Load components only when they’re needed! 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀: • 𝗥𝗲𝗮𝗰𝘁 𝗱𝗼𝗰𝘀: https://lnkd.in/dUpifpQG • 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗮𝗹 𝘁𝘂𝘁𝗼𝗿𝗶𝗮𝗹𝘀 𝗼𝗻 𝗦𝘂𝘀𝗽𝗲𝗻𝘀𝗲 + 𝗱𝗮𝘁𝗮 𝗳𝗲𝘁𝗰𝗵𝗶𝗻𝗴 #ReactJS #Suspense #DataFetching #WebDevelopment #JavaScript #Frontend #ReactDev
To view or add a comment, sign in
-
🔖React Tip: When Your “id” Isn’t Really Unique Recently ran into an interesting issue while working with API data in React. I was rendering a table and opening a popup when clicking on a cell - seemed simple enough. The API response looked like this: [ { id: "101", name: "101", age: 25 }, { id: "102", name: "102", age: 27 } ] Both `id` and `name` had the same values, and I didn’t think much of it at first. But when I opened the popup from the ID cell, it sometimes showed the wrong record or didn’t update at all. Here’s what the code looked like: {data.map((item) => ( <tr key={item.id}> <td onClick={() => openPopup(item.id)}>{item.id}</td> <td>{item.name}</td> </tr> ))} ⁉️The problem? React uses the `key` prop to identify DOM elements. If multiple rows share the same key value, React may reuse elements incorrectly. Also, since the popup logic was using `id` for lookup, it always returned the first matching record. Here’s the safer version: {data.map((item, index) => ( <tr key={index}> <td onClick={() => openPopup(item)}>{item.id}</td> <td>{item.name}</td> </tr> ))} And the popup handler: const openPopup = (rowData) => setSelected(rowData); 🧠Lesson learned: even something as small as duplicate `id` fields can cause surprising bugs in React tables. Always make sure your keys are unique and your popup or event logic doesn’t rely on repeated identifiers. #ReactJS #FrontendDevelopment #WebDevelopment #JavaScript #ReactTips
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
great work Yashika Chahal