This happen because even though the state is updated, the pop up isn't, hence the state is stale. useRef wouldn't help because useRef will not trigger a re-render, its different than useState. Try making the handleOpenModal into a react component, and pass the counter as a prop. This way, everytime the state change, the modal will change as well.In the end the modal have to re-render everytime no matter what, but I think this approach is much better than the useEffect one, which can cause unnecessary re-render.