🙅10 Common React.js Errors and How to Solve Them✨

Pratik Tamhane - Aug 21 - - Dev Community

React.js is an amazing library for building user interfaces, but even experienced developers encounter errors from time to time. Here are 10 common React errors and their solutions to help you troubleshoot effectively.

1. Invalid DOM Property

Error:
Warning: Invalid DOM property class. Did you mean className?

Cause:
In JSX, you must use className instead of class because class is a reserved keyword in JavaScript.

Solution:

<div className="container">
  {/* Correct usage */}
</div>
Enter fullscreen mode Exit fullscreen mode

2. Cannot Read Property of Undefined

Error:

TypeError: Cannot read property 'map' of undefined
Cause:
This error usually occurs when you try to access a property on an undefined object or array.

Solution:
Ensure the object or array exists before accessing its properties:

{items && items.map(item => (
  <div key={item.id}>{item.name}</div>
))}
Enter fullscreen mode Exit fullscreen mode

3. Component is Not a Function

Error:

TypeError: is not a function
Cause:
This happens when you attempt to render a component that was not correctly imported or declared.

Solution:
Ensure that the component is imported or declared correctly:

import MyComponent from './MyComponent'; // Correct import

function App() {
  return <MyComponent />;
}
Enter fullscreen mode Exit fullscreen mode

4. Objects Are Not Valid as a React Child

Error: Objects are not valid as a React child
Cause:
This error occurs when you try to render an object instead of a string, number, or JSX element.

Solution:
Convert the object to a string or display its properties:

<div>{JSON.stringify(myObject)}</div>
Enter fullscreen mode Exit fullscreen mode

5. Maximum Update Depth Exceeded

Error: Maximum update depth exceeded
Cause:
This error occurs when a component keeps re-rendering itself, often due to an unintentional loop.

Solution:
Check for infinite loops or unnecessary state updates in useEffect or componentDidUpdate:

useEffect(() => {

}, [dependencies]); 
Enter fullscreen mode Exit fullscreen mode

6. Expected a String But Got: undefined

Error:
Warning: Failed prop type: Invalid prop text of type undefined supplied to ComponentName
Cause:
You might be passing an undefined prop to a component expecting a string.

Solution:
Provide a fallback value or validate the prop:

<ComponentName text={props.text || 'Default Text'} />
Enter fullscreen mode Exit fullscreen mode

7. React Hook "useEffect" is Called Conditionally
Error: React Hook "useEffect" cannot be called conditionally
Cause:
Hooks must be called at the top level of a functional component, not inside loops, conditions, or nested functions.

Solution:
Refactor your code to ensure the hook is always called:

useEffect(() => {
  if (condition) {
    // Side-effect code
  }
}, [dependencies]);
Enter fullscreen mode Exit fullscreen mode

8. Component's Children Not Recognized

Error:
Warning: Each child in a list should have a unique "key" prop.
Cause:
When rendering lists, React requires that each child has a unique key prop.

Solution:
Provide a unique key prop to each item:

{items.map(item => (
  <div key={item.id}>{item.name}</div>
))}
Enter fullscreen mode Exit fullscreen mode

9. Invalid Hook Call Warning

Error:
Warning: Invalid hook call. Hooks can only be called inside the body of a function component.
Cause:
This error occurs when you try to call a hook outside a functional component or in the wrong context.

Solution:
Ensure hooks are only used inside functional components or custom hooks:

function MyComponent() {
  const [state, setState] = useState(0); // Correct usage
  // Component logic
}
Enter fullscreen mode Exit fullscreen mode

10. React.StrictMode Causing Side Effects Twice

Issue:
You notice that certain side effects or state updates are executed twice in development mode.

Cause:
React.StrictMode intentionally calls lifecycle methods, state setters, and effects twice to help identify unexpected side effects.

Solution:
This is a development-only behavior. To fix any issues:
Make sure side effects are idempotent.
Use the proper dependencies in useEffect.

Conclusion
Understanding these common React errors and their solutions will save you time and frustration. Bookmark this page as a quick reference for when you encounter similar issues in your projects.

Happy coding

shop Link : https://buymeacoffee.com/pratik1110r/extras

LinkedIn : https://www.linkedin.com/in/pratik-tamhane-583023217/

Behance : https://www.behance.net/pratiktamhane

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .