All 12 useState & useEffect Mistakes Junior React Developers Still Make in 2025
π Video Summary
π― Overview
This ByteGrad video details the top 12 mistakes junior React developers make when using the `useState` and `useEffect` hooks. The video aims to guide developers in avoiding these pitfalls and writing more efficient, maintainable React code. It covers a range of common errors, from incorrect state updates to improper handling of server-side rendering and fetching data.
π Main Topic
Common mistakes and best practices for using `useState` and `useEffect` in React.
π Key Points
- 1. Incorrect State Updates with Multiple `setCount` Calls [0:00]
- Solution: Use the updater function version of `useState` (e.g., `setCount(prevCount => prevCount + 1)`) to access the most up-to-date value.
- 2. Conditional Rendering of Hooks [0:11]
- Avoid using hooks inside `if` statements or other conditional blocks. - Refactor to ensure hooks are always called.
- 3. Incorrectly Updating Object State [0:47]
- Example: `setUser({...user, name: e.target.value})`. Using the updater function is also common.
- 4. Creating Separate State Variables for Every Form Input [0:09]
- Use the input's `name` attribute to dynamically update the correct form field.
- 5. Overuse of `useState` and `useEffect` for Derived State [0:26]
- Calculate derived values directly within the component's render function.
- 6. Primitives vs Non-Primitive Values [0:32]
- Primitive values (numbers, strings, booleans) are compared by value, so re-renders won't happen if the value is the same. - Objects and arrays are compared by reference, so even if the contents are the same, a new object/array will trigger a re-render.
- 7. Initializing State with Objects & Not Handling Immediate Rendering [0:39]
- Use a loading state to prevent errors when trying to access properties of undefined data.
- 8. Type Script Mistakes [0:45]
- Avoid mistakes in assigning values which typescript can catch.
- 9. Not Using Custom Hooks [0:21]
- Refactor repetitive code into reusable functions that start with "use".
- 10.Server vs. Client Components [0:30]
- `useState` and `useEffect` only work in client components. - Use `use client` directive to mark components as client-side.
- 11.Closure Issue [0:26]
- The variables inside the interval function are created at function creation time, which could lead to incorrect code. - To fix this, add the variables to the dependency array. - Use the updater function version of `useState` (e.g., `setCount(prevCount => prevCount + 1)`) to access the most up-to-date value.
- 12.Fetching Data in `useEffect` and Race Conditions [0:42]
- Use the `AbortController` to cancel previous fetch requests when a new request is made. - Consider using libraries like React Query or SWR for data fetching in client components.
π‘ Important Insights
- β’ Dependencies in `useEffect`: Carefully consider the dependencies in the `useEffect` hook's dependency array.
- β’ Client Components: Know when to use client components (`use client`) to access browser features and stateful logic.
- β’ Best Practices: Always check for edge cases to ensure the application works correctly.
π Notable Examples & Stories
- β’ Counter Component: The video uses a counter component to demonstrate incorrect state updates and the solution using the updater function [0:00].
- β’ Post Component: The video uses a post body component to show the improper usage of useEffect and fetching data [0:51].
- β’ Window Size: Using a custom hook to get the size of the window [0:27].
π Key Takeaways
- 1. Understand how `useState` and `useEffect` work under the hood.
- 2. Avoid common pitfalls like incorrect state updates, closure issues, and race conditions.
- 3. Use best practices like custom hooks, object state updates, and proper data fetching techniques.
β Action Items (if applicable)
β‘ Review and refactor existing React components to apply these best practices. β‘ Practice using custom hooks to encapsulate reusable logic. β‘ Consider using libraries like React Query or SWR for data fetching in client components.
π Conclusion
The video provides a comprehensive guide to understanding and avoiding common mistakes when using `useState` and `useEffect` in React. By mastering these concepts, developers can write cleaner, more efficient, and maintainable React code.
Create Your Own Summaries
Summarize any YouTube video with AI. Chat with videos, translate to 100+ languages, and more.
Try Free Now3 free summaries daily. No credit card required.
Summary Stats
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