Instead of "useState" try "useRef" as it avoid re-renders: const numPagesState = useRef(0);
and then you can use .current method to assign values numPagesState.current = numPages