user14367214
user14367214

Reputation:

UseEffect - React Hook useEffect has a missing dependency:

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>
);

}

Upvotes: 0

Views: 176

Answers (1)

Arun Shankar
Arun Shankar

Reputation: 89

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.

Upvotes: 1

Related Questions