0

I'm getting the following error on the useeffect hook.

React Hook useEffect has a missing dependency: 'currentPage'. Either include it or remove the dependency array.eslintreact-hooks/exhaustive-deps

Any ideas on why I'm getting this?

const Pagination = () => {
const [ page, setPage ] = useState(1);

let params = new URLSearchParams(window.location.search);
let currentPage = params.get('page') || 1;

useEffect(() => {
    setPage(currentPage)
}, []);

return (
    <div>
        <h1>{page}</h1>
        {/* 
        *
        * Skip number, current page, totalCount
        *                
        */}
    </div>
);

}

2
  • How exactly should it work? If it isn't supposed to react to param changes, params should be moved to useEffect, this removes a dep and prevents them from being calculated on every render Commented Nov 11, 2020 at 21:19
  • It's due to the ESLint rule called "react-hooks/exhaustive-deps". Explained here: reactjs.org/docs/hooks-rules.html Commented Nov 11, 2020 at 21:19

1 Answer 1

1

adding currentPage to the dependency array would remove the warning and since the value will not change unless the url changes, usseEffect will effectively be called only once.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.