React Logical Reasoning Challenge (useEffect) Predict the output of this code: import { useState, useEffect } from "react"; export default function App() { const [count, setCount] = useState(0); useEffect(() => { console.log("Effect 1:", count); setCount(count + 1); }, []); useEffect(() => { console.log("Effect 2:", count); }, [count]); return <h1>{count}</h1>; } Questions: 1 What will be printed in console? 2 What will be shown in UI? 3 How many times will component re-render? Most developers answer this wrong on first try. Drop your answers 👇 #React #JavaScript #Frontend #ReactJS #CodingChallenge
React useEffect Challenge: Predict Console Output and UI Display
More Relevant Posts
-
We had a React page that kept getting slower over time. No obvious bug. Just gradual performance drop. Here’s what we found 👇 Problem: → Page slowed down after repeated navigation → Memory usage kept increasing Root cause: → Event listeners not cleaned up → setInterval running in background → useEffect cleanup missing What I did: → Added proper cleanup functions → Removed unnecessary subscriptions → Ensured effects were scoped correctly Result: → Stable performance → No memory growth → Better user experience Insight: React doesn’t manage side effects for you. If you don’t clean up… Your app pays the price later. #ReactJS #MemoryLeak #Frontend #SoftwareEngineering #CaseStudy #JavaScript #Debugging #WebDevelopment #Engineering #Performance #FrontendDeveloper
To view or add a comment, sign in
-
Most React apps have a performance killer hiding in plain sight. It's unnecessary re-renders. Here's how to stop them: 1️⃣ Use React.memo() for pure components → Skips re-render if props haven't changed 2️⃣ useMemo() for expensive calculations → Only recalculates when dependencies change 3️⃣ useCallback() for function props → Prevents child re-renders caused by new function references 4️⃣ Lift state only where needed → Don't store everything in a top-level component 5️⃣ Use React DevTools Profiler → Visualize exactly what's re-rendering and why Bonus: The React Compiler (coming to React 19) will handle much of this automatically. But understanding the problem still makes you a better engineer. Save this for your next performance audit. 🔖 #React #ReactJS #JavaScript #WebPerformance #Frontend
To view or add a comment, sign in
-
Passing props through 4 levels of components is not a scalability problem. It's a design problem. If you have this: ``` <App> <Dashboard user={user}> <Sidebar user={user}> <UserAvatar user={user} /> </Sidebar> </Dashboard> </App> ``` Context is your friend: ```js const UserContext = createContext(null); function App() { return ( <UserContext.Provider value={user}> <Dashboard /> </UserContext.Provider> ); } function UserAvatar() { const user = useContext(UserContext); return <img src={user.avatar} />; } ``` One important caveat: Context re-renders every consumer when the value changes. For high-frequency updates, use Zustand or Redux instead. Use the right tool for the right job. #ReactJS #JavaScript #Frontend #WebDevelopment
To view or add a comment, sign in
-
React 19: Coding just got a lot easier. The new React Compiler and Actions are finally changing how we build apps. Here is why they matter: No more manual optimization: The Compiler handles performance for you. You can stop using useMemo and useCallback manually—the tool now knows what to cache. Cleaner Forms: With the useActionState hook, you don't need to write setIsLoading(true) or manage error states manually anymore. React does it for you. Faster UX: Features like useOptimistic let you update the UI instantly while the server processes in the background. The result? We’re writing less "boilerplate" code and spending more time building actual features. If you haven't tried the React 19 patterns yet, now is the perfect time to start. It makes your codebase smaller and much easier to maintain. #ReactJS #NextJS #WebDevelopment #JavaScript #CleanCode
To view or add a comment, sign in
-
-
Day 22: Error Boundaries in React In real-world applications, errors can happen anytime. A single error in one component can crash the entire UI. To handle this properly, React provides: Error Boundaries 📌 What are Error Boundaries? Error Boundaries are React components that catch JavaScript errors in their child component tree and display a fallback UI instead of crashing the whole app. ✅ Prevents complete app crash ✅ Displays fallback UI ✅ Improves user experience 📌 What Error Boundaries can catch? ✔ Errors in rendering ✔ Errors in lifecycle methods ✔ Errors in constructors of child components 📌 What Error Boundaries cannot catch? ❌ Errors inside event handlers ❌ Errors in async code (setTimeout, promises) ❌ Errors in server-side rendering 📌 Example of an Error Boundary import React from "react"; class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { return { hasError: true }; } componentDidCatch(error, info) { console.log("Error:", error); console.log("Info:", info); } render() { if (this.state.hasError) { return <h2>Something went wrong!</h2>; } return this.props.children; } } export default ErrorBoundary; 📌 How to use it? import ErrorBoundary from "./ErrorBoundary"; import App from "./App"; function Main() { return ( <ErrorBoundary> <App /> </ErrorBoundary> ); } Now, if any child component crashes, the app will show: 👉 "Something went wrong!" 📌 Why Error Boundaries are important? 🔥 Used in production apps 🔥 Prevents blank screens 🔥 Helps debugging errors #ReactJS #ErrorBoundary #FrontendDevelopment #JavaScript #WebDevelopment #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
Why does your app "freeze" during big tasks? Even with 5 years of experience, this one still trips people up. JavaScript is "single-threaded." This means it can only do one thing at a time. The Problem: If you run a heavy calculation, the app cannot "draw" the UI or handle touches until that task is finished. Example (The Bad Way): // This freezes the screen for 5 seconds! const processData = () => { for(let i = 0; i < 1000000000; i++) { // Heavy work... } console.log("Done!"); } The Fix: Break big tasks into small pieces or move them to a "Worker." In React Native, keep the "Main Thread" free so your animations stay smooth. Senior Rule: Never block the UI. If a task takes more than 100ms, it shouldn't be on the main thread. #JavaScript #ReactNative #Coding #Performance #SimpleCoding
To view or add a comment, sign in
-
🧠 Part 3 of 10: Duplicated state is one of the fastest ways to make a React app feel unstable. Everything looks fine at first. Then one value updates. The other one doesn’t. Now the UI technically works, but nobody fully trusts it. That’s the part people don’t say enough: a lot of frontend bugs are really trust issues. The UI says one thing. The data says another. Now the team starts building around confusion. Whenever I can, I try to keep state close to a single source of truth. It makes code easier to reason about. And future changes get a lot less annoying. What bug have you traced back to duplicated state? #React #ReactJS #FrontendEngineering #StateManagement #JavaScript #UIEngineering #WebDevelopment
To view or add a comment, sign in
-
Most React apps are slower than they should be. And it's usually because of these mistakes: The problem nobody tells you: Every re-render costs performance. And most devs trigger them without even realizing it. Here's what to fix: → Stop putting objects/arrays directly in JSX props They create a new reference on every render — use useMemo instead. → Wrap expensive components in React.memo Prevents re-renders when props haven't changed. → Don't define functions inside JSX Use useCallback to keep function references stable. → Avoid anonymous functions in useEffect dependencies They break memoization silently. → Use lazy loading for heavy components import React, { lazy, Suspense } your initial bundle will thank you. Profile before you optimize Use React DevTools Profiler to find ACTUAL bottlenecks — don't guess. #React #JavaScript #Frontend #WebDevelopment #ReactJS #Performance #MERN #DevTips
To view or add a comment, sign in
-
⚡ Stop making your React apps slow… without realizing it Most developers write code like this 👇 Recalculating data on every render 😬 Result? 🐢 Slower UI ❌ Unnecessary re-renders ⚠️ Poor performance Then comes the game changer: 👉 Memoization (useMemo) Suddenly: ⚡ Faster rendering ⚡ Better performance ⚡ Optimized user experience 💡 Small optimization → Big impact If you're working with React, start thinking about performance early not after your app slows down. 🔥 Have you ever faced performance issues like this? #React #JavaScript #WebDevelopment #Frontend #Performance #CleanCode #Developers #Coding #Tech #Programming #ReactJS #DevTips
To view or add a comment, sign in
-
-
Most developers try to optimize React apps by writing better code… But the real problem? 👉 Everything runs at the same priority. I recently fixed a laggy search feature (10k+ items): Before 😓 Typing = lag After 😎 Typing = smooth Results = deferred All thanks to useTransition. ⚡ Concurrent Rendering isn’t about speed. It’s about user experience. If your UI feels slow, this might be the missing piece. #ReactJS #Frontend #Performance #WebDev #JavaScript #UIDesign
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
Hai