📢 Advertisement Placeholder
Slot: SEO_PAGE_TOP | Format: horizontal
Google AdSense will appear here once approved

All 12 useState & useEffect Mistakes Junior React Developers Still Make in 2025

ByteGrad
46 min
1 views

📋 Video Summary

🎯 Overview

This video from ByteGrad discusses the top 12 mistakes junior React developers make when using the `useState` and `useEffect` hooks. It provides practical examples and solutions to common pitfalls, helping viewers write cleaner, more efficient, and professional React code.

📌 Main Topic

Common mistakes and best practices for using `useState` and `useEffect` in React.

🔑 Key Points

  • 1. Incorrectly Updating State with Multiple Calls [0:00]
Calling `setState` multiple times in quick succession doesn't always update the state as expected.

Use the updater function (e.g., `setCount(prevCount => prevCount + 1)`) to ensure you're working with the most up-to-date state value.

  • 2. Conditional Rendering and Hooks [0:35]
Avoid calling hooks (like `useState` and `useEffect`) conditionally (inside `if` statements). This can lead to React errors.

Ensure hooks are always called in the same order during every render.

  • 3. Incorrectly Updating Object State [0:47]
When updating objects in state, don't directly modify the state object properties (e.g., `user.name = e.target.value`).

Use the spread operator (`...`) to create a new object, copying existing properties, and then override the desired property (e.g., `setUser({ ...user, name: e.target.value })`).

  • 4. Inefficient State Management for Forms [0:59]
Instead of separate state variables for each form input, use a single object to manage the entire form's state.

Use the `name` attribute on input fields to dynamically update the form state (e.g., `onChange={e => setForm({ ...form, [e.target.name]: e.target.value })}`).

  • 5. Overuse of `useState` and `useEffect` for Derived State [1:16]
Don't create new state variables and use `useEffect` if the value can be derived directly from existing state.

Calculate derived values (like a full name or total price) within the component's function body.

  • 6. Primitives vs. Non-Primitives and Re-renders [1:32]
React components only re-render if the state has changed.

Primitive values (numbers, strings, booleans) are compared by value, so if their values are identical, no re-render occurs. Objects and arrays are compared by reference. Even if the content of two objects is the same, they are different objects, so a re-render will occur.

  • 7. Incorrectly Initializing State with Objects [1:42]
When fetching data and setting an object in state, initially set the state to `null`.

Use optional chaining (`?.`) to safely access properties of the object while it's still loading. Implement a loading state to prevent errors and improve the user experience.

  • 8. Type Safety with TypeScript [1:50]
Use TypeScript to define types for your state variables.

TypeScript can infer types for primitive values. For objects, define a type (e.g., `type Post = { title: string; body: string; }`) and use it when setting the state.

  • 9. Not Using Custom Hooks [2:06]
Refactor repetitive logic, especially involving `useState` and `useEffect`, into custom hooks.

Custom hooks make code reusable and easier to maintain.

  • 10.Server and Client Components [2:11]
Understand the difference between server and client components in Next.js.

`useState` and `useEffect` can only be used in client components. Use `use client` directive at the top of the component to make it a client component.

  • 11.Closure and useEffect [2:18]
Be aware of closures in JavaScript. Variables in `useEffect` capture the value at the time the effect is created.

To get the updated value, include the variable in the dependency array or use the updater function for `setState`. To prevent memory leaks cancel any pending intervals using the cleanup function.

  • 12.Fetching Data in useEffect [2:20]
Avoid fetching data directly in `useEffect`.

If fetching data in a client component, consider using libraries like React Query or SWR to handle caching, race conditions, loading states, and error states. To prevent race conditions, use an abort controller to cancel previous fetch requests before initiating a new one.

💡 Important Insights

  • Understanding Closures: [2:18] The concept of closures is crucial for understanding how variables are accessed within `useEffect` and how to ensure you're working with the correct, up-to-date values.
  • Dependency Arrays: [1:50] Carefully manage the dependencies in `useEffect` to avoid unnecessary re-renders and ensure the effect runs at the right times.
  • The Dry Principle: [2:06] Don't Repeat Yourself (DRY) principle applies to React. Refactor repeated code into custom hooks.

📖 Notable Examples & Stories

  • Counter Component Example: [0:00] The video uses a simple counter example to demonstrate the issue of multiple `setState` calls and how to solve it with the updater function.
  • Product Card Example: [0:35] Demonstrates the problem of calling hooks conditionally and how to fix it by ensuring hooks are called in the same order.
  • Object State Update Example: [0:47] Shows the common mistake of incorrectly updating object state and provides the solution using the spread operator.
  • Form Example: [0:59] Illustrates how to efficiently manage form state using a single object and the `name` attribute.
  • Loading State Example: [1:42] Shows how to handle loading states when fetching data and initializing the state.
  • Post Body Component Example: [3:51] Illustrates race conditions when fetching data with the ID and how to solve them.

🎓 Key Takeaways

  • 1. Use the updater function when you need to update state based on its previous value.
  • 2. Avoid calling hooks conditionally and ensure they are called in the same order every time.
  • 3. Use the spread operator when updating objects in state.
  • 4. Refactor repetitive logic into custom hooks for reusability.
  • 5. Be mindful of server and client components in modern React frameworks.
  • 6. Understand closures and dependency arrays for effective `useEffect` usage.
  • 7. Consider using libraries or server components for data fetching to handle edge cases.

✅ Action Items

□ Review and refactor existing React components to apply the best practices discussed. □ Experiment with custom hooks to improve code reusability. □ Explore data fetching strategies using React Query, SWR, or server components. □ Deepen your understanding of JavaScript fundamentals, especially closures and the spread operator.

🔍 Conclusion

The video provides a comprehensive guide to avoiding common mistakes when using `useState` and `useEffect` in React. By understanding these pitfalls and implementing the recommended solutions, junior developers can write cleaner, more efficient, and maintainable code, leading to a smoother and more professional development experience.

📢 Advertisement Placeholder
Slot: SEO_PAGE_BOTTOM | Format: horizontal
Google AdSense will appear here once approved

Create Your Own Summaries

Summarize any YouTube video with AI. Chat with videos, translate to 100+ languages, and more.

Try Free Now

3 free summaries daily. No credit card required.

Summary Stats

Views 1
Shares
Created Nov 13, 2025
📢 Advertisement Placeholder
Slot: SEO_PAGE_SIDEBAR | Format: vertical
Google AdSense will appear here once approved

What You Can Do

  • Chat with Video

    Ask questions about content

  • Translate

    Convert to 100+ languages

  • Export to Notion

    Save to your workspace

  • 12 Templates

    Study guides, notes, blog posts

See All Features