💭 𝐄𝐯𝐞𝐫 𝐭𝐫𝐢𝐞𝐝 𝐛𝐮𝐢𝐥𝐝𝐢𝐧𝐠 𝐚𝐧 𝐔𝐧𝐝𝐨-𝐑𝐞𝐝𝐨 𝐟𝐞𝐚𝐭𝐮𝐫𝐞 𝐟𝐫𝐨𝐦 𝐬𝐜𝐫𝐚𝐭𝐜𝐡 𝐢𝐧 𝐑𝐞𝐚𝐜𝐭? I recently solved this interesting question by Akshay Saini 🚀 — and it turned into a great hands-on way to understand how React manages state, history, and immutability. 🎯 𝗧𝗵𝗲 𝗚𝗼𝗮𝗹 To build a text editor where we can move the state both backward (Undo) and forward (Redo) — just like in a real text editor 📝. ⚙️ 𝗧𝗵𝗲 𝗖𝗼𝗿𝗲 𝗖𝗼𝗻𝗰𝗲𝗽𝘁 Think of it like managing a timeline of states. Each time you type, React stores a snapshot of that text in a history array. We keep track of: 👉 text → current value 👉 history → list of all text snapshots 👉 currentIndex → pointer to where we are in the timeline When you: 🔹 𝐓𝐲𝐩𝐞 𝐬𝐨𝐦𝐞𝐭𝐡𝐢𝐧𝐠 𝐧𝐞𝐰: Add it to history and move forward. 🔹 𝐔𝐧𝐝𝐨: Move one step backward and show an older value. 🔹 𝐑𝐞𝐝𝐨: Move one step forward again to the latest value. If you type something after undoing, the “future” history is removed — you’ve now created a new branch of edits 🔁 💡 𝗛𝗶𝘀𝘁𝗼𝗿𝘆 𝗜𝗻 𝗔𝗰𝘁𝗶𝗼𝗻 a ➡️ ["a"] (currentIndex = 0) am ➡️ ["a", "am"] (currentIndex = 1) ama ➡️ ["a", "am", "ama"] (currentIndex = 2) Undo ⏪ → back to "am" amf ➡️ ["a", "am", "amf"] (currentIndex = 2) ✅ If we don’t remove the future states after undo, redo stops working — because we’re mixing old and new timelines 💥 That’s the trick: always keep a clean timeline of history 🕓 🧠 𝗪𝗵𝗮𝘁 𝗜 𝗟𝗲𝗮𝗿𝗻𝗲𝗱 This challenge gave me a much deeper understanding of how time-travel state management works — the same concept that powers Redux DevTools 💻 𝐅𝐮𝐥𝐥 𝐪𝐮𝐞𝐬𝐭𝐢𝐨𝐧 : https://lnkd.in/dt3Yn3pJ 🔗 𝗰𝗼𝗱𝗲: https://lnkd.in/dpuRtiuj 💬 What would you do differently — or how would you optimize this approach? Drop your thoughts below 👇 #ReactJS #FrontendDevelopment #JavaScript #WebDevelopment #StateManagement #CodeLearning
Building an Undo Feature in React
More Relevant Posts
-
𝗛𝗼𝘄 𝗜 𝗜𝗺𝗽𝗿𝗼𝘃𝗲𝗱 𝗥𝗲𝗮𝗰𝘁 𝗔𝗽𝗽 𝗣𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 𝗪𝗶𝘁𝗵𝗼𝘂𝘁 𝗖𝗵𝗮𝗻𝗴𝗶𝗻𝗴 𝘁𝗵𝗲 𝗨𝗜 Performance isn’t always about redesigning; sometimes, it’s about rethinking. I once worked on a React project that looked fine but felt sluggish. Components re-rendered too often, and users felt the lag. Here’s what helped me optimize it without rewriting everything 👇 𝗨𝘀𝗲 𝗥𝗲𝗮𝗰𝘁.𝗺𝗲𝗺𝗼: Prevents re-renders when props don’t change. 𝗨𝘀𝗲 𝘂𝘀𝗲𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸 𝗮𝗻𝗱 𝘂𝘀𝗲𝗠𝗲𝗺𝗼: Keeps functions and values stable between renders. 𝗖𝗼𝗱𝗲 𝘀𝗽𝗹𝗶𝘁𝘁𝗶𝗻𝗴: Load only what’s needed with dynamic imports. 𝗟𝗮𝘇𝘆 𝗹𝗼𝗮𝗱𝗶𝗻𝗴: Defer non-critical components to speed up the initial load. 𝗔𝗻𝗮𝗹𝘆𝘇𝗲 𝗯𝗲𝗳𝗼𝗿𝗲 𝗴𝘂𝗲𝘀𝘀𝗶𝗻𝗴: Tools like React Profiler help you see where the slowdown actually happens. These small optimizations dropped the load time by 𝟰𝟬% and users immediately felt the difference. 𝗙𝗿𝗼𝗻𝘁𝗲𝗻𝗱 𝗽𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 𝗶𝘀 𝗻𝗼𝘁 𝗺𝗮𝗴𝗶𝗰, 𝗶𝘁’𝘀 𝗱𝗶𝘀𝗰𝗶𝗽𝗹𝗶𝗻𝗲. #Reactjs #FrontendPerformance #WebDevelopment #Nextjs #Optimization #JavaScript #FullStackDeveloper #CodingTips #FrontendEngineer
To view or add a comment, sign in
-
-
⚛️ React Just Made Form Actions Way Cleaner React’s new hook — useActionState — is a game-changer for handling async form submissions. No more juggling useState, useEffect, or endless try/catch blocks. 🙌 Here’s what it does 👇 🧩 You pass it: A form action (e.g., addToCart) An initial state It gives you back three things: 1️⃣ The latest state (e.g., message or result) 2️⃣ A wrapped action (formAction) 3️⃣ A flag showing if it’s still running (isPending) Now your form logic becomes simpler, more declarative, and easier to read. Just write the action, hook it up, and React handles the rest. It’s a small addition but one that makes a big difference in building clean, async-ready UIs. ⚡ 💬 Have you tried useActionState yet? What’s your take on React’s direction with these new declarative patterns? #ReactJS #JavaScript #WebDevelopment #Frontend #ReactHooks #CleanCode #AsyncProgramming #DeveloperExperience #SoftwareEngineering #CodingTips #ReactDevelopers #DevCommunity #UIUX
To view or add a comment, sign in
-
-
⚛️ React Just Made Form Actions So Much Cleaner The new useActionState hook is a game-changer for handling async form submissions. No more juggling useState, useEffect, or endless try/catch blocks. 🙌 Here’s how it works 👇 🧩 You provide: A form action (e.g., addToCart) An initial state And React gives you back: 1️⃣ The latest state (like a message or result) 2️⃣ A wrapped form action (formAction) 3️⃣ A flag showing if it’s still running (isPending) This means your form logic becomes simpler, more declarative, and much easier to read. Just write the action, hook it up, and React handles the rest. A small API — but it makes a big difference for building clean, async-ready UIs. ⚡ 💬 Have you tried useActionState yet? What do you think about React’s new declarative direction? #ReactJS #JavaScript #WebDevelopment #Frontend #ReactHooks #CleanCode Dhruv Patel (Borad)
To view or add a comment, sign in
-
-
𝙍𝙚𝙖𝙘𝙩 𝙃𝙤𝙤𝙠𝙨 𝙄 𝙒𝙞𝙨𝙝 𝙄 𝙆𝙣𝙚𝙬 𝙀𝙖𝙧𝙡𝙞𝙚𝙧 Let’s be honest, when you first start building with React, 𝘂𝘀𝗲𝗦𝘁𝗮𝘁𝗲 and 𝘂𝘀𝗲𝗘𝗳𝗳𝗲𝗰𝘁 feel like superheroes. Then you discover 𝘂𝘀𝗲𝗠𝗲𝗺𝗼 and 𝘂𝘀𝗲𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸, and you’re like, “Yeah, I’ve cracked the React code.” Well… not quite. 😂 There are a few underrated hooks that completely changed how I write cleaner, smarter, and more efficient components. Here are 3 that deserve way more hype: 1. 𝘂𝘀𝗲𝗜𝗺𝗽𝗲𝗿𝗮𝘁𝗶𝘃𝗲𝗛𝗮𝗻𝗱𝗹𝗲: Lets you control what a parent can access from a child component. No more unnecessary prop drilling chaos. 2. 𝘂𝘀𝗲𝗟𝗮𝘆𝗼𝘂𝘁𝗘𝗳𝗳𝗲𝗰𝘁: Perfect when you need to measure or adjust the DOM before it renders on screen. No more layout flickers or surprise jumps. 3. 𝘂𝘀𝗲𝗗𝗲𝗳𝗲𝗿𝗿𝗲𝗱𝗩𝗮𝗹𝘂𝗲: Keeps your UI smooth during heavy renders. I’ve used it in large forms and search bars, and the difference was night and day. These hooks didn’t just simplify my code, they made my development flow cleaner and more enjoyable. I’ve shared a few of these experiments and mini-projects on my GitHub ( https://lnkd.in/eP9nmTEw ) if you love exploring real-world React setups. Building products that scale and perform well (no matter where in the world the team is) has become something I genuinely enjoy diving into. What about you, which React hook do you think doesn’t get enough credit? #ReactJS #FullstackDeveloper #WebDevelopment #Frontend #ReactHooks #JavaScript #CodingHumor #CleanCode #DevLife #RemoteWork #GitHub
To view or add a comment, sign in
-
-
💡 A powerful (and often underrated) use case for useRef in React useRef isn’t just about accessing DOM elements — it quietly solves one of the most important problems in React: preserving mutable values across re-renders without triggering re-renders. A classic example is a stopwatch ⏱️ const timerRef = useRef(null); const start = () => { timerRef.current = setInterval(() => { setSeconds((s) => s + 1); }, 1000); }; const stop = () => { clearInterval(timerRef.current); }; If we used a normal variable like let timer, React would reset it to null after every re-render (since the component function runs again). The result? Your “Stop” button wouldn’t actually stop the timer — the interval would keep running in the background. useRef prevents that by giving you a stable place to store such values — perfect for things like: Intervals / timeouts WebSocket or media stream instances Storing previous values Keeping track of imperative API references It’s one of those hooks that doesn’t re-render the UI, but silently keeps your logic rock solid. ⚙️✨ Sometimes, the most powerful tools are the quiet ones. #ReactJS #FrontendDevelopment #WebDev #JavaScript #useRef
To view or add a comment, sign in
-
𝗥𝗲𝗮𝗰𝘁 𝗡𝗮𝘁𝗶𝘃𝗲 0.82 𝗶𝗻𝘁𝗿𝗼𝗱𝘂𝗰𝗲𝘀 𝗗𝗢𝗠-𝗹𝗶𝗸𝗲 𝗡𝗼𝗱𝗲 𝗔𝗣𝗜𝘀 𝘃𝗶𝗮 𝗿𝗲𝗳𝘀! For years, React Native refs gave us 𝗹𝗶𝗺𝗶𝘁𝗲𝗱 𝗻𝗮𝘁𝗶𝘃𝗲 𝗵𝗮𝗻𝗱𝗹𝗲𝘀 — only a few methods like: • measure() • setNativeProps() Now, starting 𝗥𝗲𝗮𝗰𝘁 𝗡𝗮𝘁𝗶𝘃𝗲 0.82, refs on native components will return 𝗗𝗢𝗠-𝗹𝗶𝗸𝗲 𝗻𝗼𝗱𝗲𝘀, bringing the Web and Native worlds closer than ever. 𝗪𝗵𝗮𝘁’𝘀 𝗡𝗲𝘄 ✅ DOM-like structure navigation (parentNode, children) ✅ Layout measurement via getBoundingClientRect() ✅ ownerDocument + getElementById() support ✅ Access to Text and root document nodes ✅ 𝗕𝗮𝗰𝗸𝘄𝗮𝗿𝗱 𝗰𝗼𝗺𝗽𝗮𝘁𝗶𝗯𝗹𝗲: old APIs like measure & setNativeProps still work 𝗪𝗵𝘆 𝗧𝗵𝗶𝘀 𝗠𝗮𝘁𝘁𝗲𝗿𝘀 • One consistent API for 𝗪𝗲𝗯 + 𝗡𝗮𝘁𝗶𝘃𝗲 • Cleaner, modern way to traverse and measure the UI tree • Foundation for 𝗰𝗿𝗼𝘀𝘀-𝗽𝗹𝗮𝘁𝗳𝗼𝗿𝗺 𝗨𝗜 𝗹𝗶𝗯𝗿𝗮𝗿𝗶𝗲𝘀 and 𝘂𝗻𝗶𝗳𝗶𝗲𝗱 𝘁𝗼𝗼𝗹𝗶𝗻𝗴 This update finally makes 𝗥𝗲𝗮𝗰𝘁 𝗡𝗮𝘁𝗶𝘃𝗲 𝗳𝗲𝗲𝗹 𝗹𝗶𝗸𝗲 𝘁𝗵𝗲 𝗗𝗢𝗠 — no more weird native refs or imperative hacks. #ReactNative #ReactJS #React19 #JavaScript #MobileDevelopment #Frontend #WebDev #CrossPlatform #OpenSource
To view or add a comment, sign in
-
-
⚡𝗪𝗲 𝗮𝗹𝗹 𝘂𝘀𝗲 𝙪𝙨𝙚𝙎𝙩𝙖𝙩𝙚... 𝗯𝘂𝘁 𝗱𝗼 𝘆𝗼𝘂 𝗿𝗲𝗮𝗹𝗹𝘆 𝗸𝗻𝗼𝘄 𝘄𝗵𝗮𝘁 𝗵𝗮𝗽𝗽𝗲𝗻𝘀 𝘄𝗵𝗲𝗻 𝘆𝗼𝘂 𝗰𝗹𝗶𝗰𝗸 𝘁𝗵𝗮𝘁 𝗯𝘂𝘁𝘁𝗼𝗻? If you’ve ever built a form, a toggle, or even a theme switcher in React — chances are, 𝘂𝘀𝗲𝗦𝘁𝗮𝘁𝗲 was your first hook friend 💡 Let’s decode it — not just how to use it, but how it actually works behind the scenes. 💡 𝙒𝙝𝙖𝙩 𝙞𝙨 𝙪𝙨𝙚𝙎𝙩𝙖𝙩𝙚? useState is a special React Hook that lets you add state to functional components.Before hooks, only class components could manage state. Now with useState, every component can remember values and react to changes all without writing classes. 🧠 what happens in the example: • 𝘠𝘰𝘶 𝘪𝘯𝘪𝘵𝘪𝘢𝘭𝘪𝘻𝘦 𝘴𝘵𝘢𝘵𝘦 → 𝘶𝘴𝘦𝘚𝘵𝘢𝘵𝘦(𝘧𝘢𝘭𝘴𝘦) • 𝘙𝘦𝘢𝘤𝘵 𝘴𝘵𝘰𝘳𝘦𝘴 𝘵𝘩𝘪𝘴 𝘷𝘢𝘭𝘶𝘦 𝘪𝘯𝘵𝘦𝘳𝘯𝘢𝘭𝘭𝘺 • 𝘞𝘩𝘦𝘯 𝘺𝘰𝘶 𝘤𝘢𝘭𝘭 𝘴𝘦𝘵𝘐𝘴𝘋𝘢𝘳𝘬(!𝘪𝘴𝘋𝘢𝘳𝘬) → 𝘙𝘦𝘢𝘤𝘵 𝘶𝘱𝘥𝘢𝘵𝘦𝘴 𝘵𝘩𝘦 𝘴𝘵𝘢𝘵𝘦 𝘢𝘯𝘥 𝘳𝘦-𝘳𝘦𝘯𝘥𝘦𝘳𝘴 𝘵𝘩𝘦 𝘤𝘰𝘮𝘱𝘰𝘯𝘦𝘯𝘵 • 𝘛𝘩𝘦 𝘜𝘐 𝘯𝘰𝘸 𝘳𝘦𝘧𝘭𝘦𝘤𝘵𝘴 𝘵𝘩𝘦 𝘶𝘱𝘥𝘢𝘵𝘦𝘥 𝘵𝘩𝘦𝘮𝘦 𝘪𝘯𝘴𝘵𝘢𝘯𝘵𝘭𝘺 𝗧𝗵𝗮𝘁’𝘀 𝘁𝗵𝗲 𝗺𝗮𝗴𝗶𝗰 𝗼𝗳 𝗥𝗲𝗮𝗰𝘁’𝘀 𝗱𝗲𝗰𝗹𝗮𝗿𝗮𝘁𝗶𝘃𝗲 𝗻𝗮𝘁𝘂𝗿𝗲 ✨ 🧩 𝗖𝗼𝗺𝗺𝗼𝗻 𝗠𝗶𝘀𝘁𝗮𝗸𝗲𝘀 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿𝘀 𝗠𝗮𝗸𝗲 ❌ Trying to update state directly → isDark = true ✅ Always use the updater function → setIsDark(true) ❌ Expecting state to update instantly after calling setState ✅ React batches updates — so don’t log the value immediately after calling setIsDark. 𝘌𝘷𝘦𝘳𝘺 𝘵𝘪𝘮𝘦 𝘺𝘰𝘶𝘳 𝘤𝘰𝘮𝘱𝘰𝘯𝘦𝘯𝘵 𝘳𝘦𝘯𝘥𝘦𝘳𝘴, 𝘶𝘴𝘦𝘚𝘵𝘢𝘵𝘦 𝘨𝘪𝘷𝘦𝘴 𝘺𝘰𝘶 𝘵𝘩𝘦 𝘤𝘶𝘳𝘳𝘦𝘯𝘵 𝘴𝘵𝘢𝘵𝘦 𝘧𝘳𝘰𝘮 𝘙𝘦𝘢𝘤𝘵’𝘴 𝘪𝘯𝘵𝘦𝘳𝘯𝘢𝘭 𝘮𝘦𝘮𝘰𝘳𝘺. 𝘞𝘩𝘦𝘯 𝘴𝘦𝘵𝘚𝘵𝘢𝘵𝘦 𝘪𝘴 𝘤𝘢𝘭𝘭𝘦𝘥, 𝘙𝘦𝘢𝘤𝘵 𝘴𝘤𝘩𝘦𝘥𝘶𝘭𝘦𝘴 𝘢 𝘳𝘦-𝘳𝘦𝘯𝘥𝘦𝘳 𝘸𝘪𝘵𝘩 𝘵𝘩𝘦 𝘯𝘦𝘸 𝘷𝘢𝘭𝘶𝘦 — 𝘢𝘯𝘥 𝘶𝘱𝘥𝘢𝘵𝘦𝘴 𝘰𝘯𝘭𝘺 𝘸𝘩𝘢𝘵 𝘤𝘩𝘢𝘯𝘨𝘦𝘥. 𝗧𝗵𝗮𝘁’𝘀 𝗵𝗼𝘄 𝗥𝗲𝗮𝗰𝘁 𝘀𝘁𝗮𝘆𝘀 𝗳𝗮𝘀𝘁 ⚡ 💬 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀 𝘁𝗼 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 1️⃣ What is the difference between initializing state directly vs through useState? 2️⃣ What happens internally when setState is called? 3️⃣ Why should we avoid updating state directly? 🔖 #ReactJS #ReactHooks #FrontendDeveloper #MERNStack #JavaScript #WebDevelopment #LearningSeries #LinkedInLearning #useState #ReactTips #JavaScript #ReactDeveloper #CodingJourney #InterviewPreparation #CareerGrowth #FrontendInterview #WebDevCommunity #DeveloperLife
To view or add a comment, sign in
-
-
🚀 Removing DOM Elements (JavaScript) You can remove DOM elements using the `removeChild()` method, which removes a child node from its parent. Alternatively, you can use the `remove()` method, which removes the element from the DOM directly. Before removing an element, ensure that you no longer need it, as removing it permanently removes it from the DOM tree. Removing elements dynamically is important for updating the UI based on user actions or data changes, maintaining a clean and efficient DOM structure. 🚀 Every concept learned is a step toward mastery! 🎓 Master any tech topic — 10,000+ concise concepts, 4,000+ articles, 12,000+ practice questions. AI-powered! ⚡ Join thousands: https://lnkd.in/gefySfsc 🌐 Visit us: https://techielearn.in #JavaScript #WebDev #Frontend #JS #professional #career #development
To view or add a comment, sign in
-
-
Master React Hooks with clarity and purpose I recently saw some posts explaning abut React Hooks and I would like to bring a post talking about it to remember how they work. Here some of them that I use on my dailys - useState: manage local state inside functional components. - useEffect: handle side effects like data fetching or subscriptions. - useRef: keep mutable values between renders or reference DOM elements. - useContext: share data across components without prop drilling. - useReducer: a great choice for complex state logic. - useMemo & useCallback: prevent unnecessary recalculations and re-renders. - useLayoutEffect: runs before the browser paints, useful for DOM measurements. - Advanced ones like useDebugValue or useImperativeHandle serve specific use cases. 💡 My thoughts As a full-stack developer, I’m always chasing cleaner and more scalable front-end patterns. React Hooks, when used thoughtfully, make components more modular, predictable, and performant. 🚀 Pro tip If you’re learning React or mentoring others, pick one hook per day and test it in a small component. Real experimentation beats memorization. #React #Frontend #WebDevelopment #Hooks #JavaScript #NextJS #DevTips #CleanCode
To view or add a comment, sign in
-
💡 𝗘𝘃𝗲𝗿𝘆 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 𝗛𝗮𝘀 𝗙𝗮𝗰𝗲𝗱 𝗧𝗵𝗶𝘀 𝗠𝗼𝗺𝗲𝗻𝘁 😅 You’ve finished building the frontend. Everything looks perfect animations smooth, layout pixel-perfect… But when you click “𝗙𝗲𝘁𝗰𝗵 𝗗𝗮𝘁𝗮” 💥 404: API Not Found. That’s the moment every developer knows — when your work depends on something that’s not ready yet. You wait. You refresh. You message the backend dev. And they reply with the classic line — “API will be ready soon 😅” But here’s the thing… You don’t have to wait anymore. With 𝗷𝘀𝗼𝗻𝘁𝗼𝗮𝗹𝗹. 𝘁𝗼𝗼𝗹𝘀 you can create 𝗠𝗼𝗰𝗸 𝗔𝗣𝗜𝘀 𝗶𝗻𝘀𝘁𝗮𝗻𝘁𝗹𝘆 from your JSON or schema. No backend, no delay, no blockers. Now the frontend runs. The flow stays smooth. And your sprint doesn’t stop just because the API did. 🚀 👉 Try it yourself — 𝗺𝗼𝗰𝗸 𝗶𝘁, 𝘁𝗲𝘀𝘁 𝗶𝘁, 𝘀𝗵𝗶𝗽 𝗶𝘁. Because waiting for APIs? That’s history. #developerlife #frontend #backend #mockapi #webdevelopment #angular #react #javascript #jsontoall #softwareengineer #productivity
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