AskHandle

AskHandle Blog

How to Resolve "React Hook useEffect has a missing dependency" in ESLint

September 4, 2025Melissa Olson3 min read

How to Resolve "React Hook useEffect has a missing dependency" in ESLint

Are you facing the error "React Hook useEffect has a missing dependency" while using ESLint in your React projects? This is a common issue, but it can be resolved easily. Let's explore why this error occurs and how to fix it.

The Error: Understanding the Issue

The message "React Hook useEffect has a missing dependency" indicates that ESLint has detected a missing dependency in the dependency array of the useEffect hook in your React component.

The useEffect hook helps handle side effects in functional components. It allows operations such as data fetching, subscriptions, or manual DOM updates. The dependency array specifies the variables that the effect depends on. Omitting a dependency used in the effect leads ESLint to flag a warning.

Why is it Important to Fix This Error?

It is important to fix this issue, even if the code appears to work correctly at first. Missing dependencies can result in bugs and inconsistencies later. Addressing this error enhances the stability and maintainability of your codebase.

Resolving the Error: Practical Solutions

You can resolve the "React Hook useEffect has a missing dependency" error in several ways. Here are practical solutions to implement in your React components:

1. Include All Necessary Dependencies

The simplest solution is to include all dependencies used inside the useEffect hook in the dependency array. This informs React to re-run the effect whenever any of these values change.

jsx
1import React, { useEffect, useState } from 'react';
2
3const ExampleComponent = () => {
4  const [data, setData] = useState(null);
5  const fetchData = async () => {
6    // Fetch data from an [API](/glossary/api)
7  };
8
9  useEffect(() => {
10    fetchData();
11  }, [fetchData]); // Include all dependencies here
12};

2. Use useCallback for Functions

If you use a function as a dependency in your useEffect hook, wrap it with the useCallback hook. This maintains the function reference across renders, preventing unnecessary re-renders due to changing function references.

jsx
1import React, { useEffect, useState, useCallback } from 'react';
2
3const ExampleComponent = () => {
4  const [data, setData] = useState(null);
5
6  const fetchData = useCallback(async () => {
7    // Fetch data from an API
8  }, []);
9
10  useEffect(() => {
11    fetchData();
12  }, [fetchData]);
13};

3. Disable ESLint Rule (Use with Caution)

Sometimes, it may be necessary to disable the ESLint rule that issues the warning. This should be a last resort if refactoring is not an option. To turn off the rule, add // eslint-disable-next-line react-hooks/exhaustive-deps above the useEffect declaration.

jsx
1useEffect(() => {
2  fetchData();
3  // eslint-disable-next-line react-hooks/exhaustive-deps
4}, []);

Best Practices to Avoid Future Errors

To prevent the "React Hook useEffect has a missing dependency" error, consider these best practices:

  • Always include all dependencies used in the useEffect hook in the dependency array.
  • Use the useCallback hook to memoize functions that are dependencies of the useEffect hook.
  • Regularly review your codebase for ESLint warnings and address them promptly to maintain code quality.

The "React Hook useEffect has a missing dependency" error in ESLint can be resolved by paying attention to the dependencies declared in the useEffect hook. By following the solutions and best practices outlined here, you can effectively tackle this error and improve the quality of your React projects.