Open sapkra opened 2 years ago
Awesome, thanks for sharing this.
I really like the idea of storing the lastFetched
Date in a ref. There's no benefit of using React state here since the date is not used during render, so the ref idea is great! 👍
I think it makes sense to stick with use-window-focus
though, even if it triggers re-renders on focus. In my experience it's easier to rely on libraries for this functionality, even if the behavior isn't perfect for the use case (ie triggers a re-render when we don't need it to).
Are there any problems with your app caused by focus triggered re-renders?
If you don't want your app component to re-render one suggestion would be to put the call to useHasNewDeploy()
call in it's own component, so only that component will re-render.
Something like
App = () => {
return (
<div>
{/* My app here */}
<DeployNotifications />
</div>
);
}
let DeployNotifications = () => {
let { hasNewDeploy } = useHasNewDeploy();
// .. whatever UI for you need for displaying notifications
}
Again thanks for sharing this and I'll update this library to use a ref for last fetched.
I'm glad that you like my code and thanks for the feedback. But I disagree with your opinion about using external libraries for extremely simple event handlers, explicitly if the library is triggering unexpected behaviors. It's always better to have no dependencies at all, also if you think about security.
The re-renders triggered that some form elements became empty for some reason or prefilled with the browser autocomplete again.
The tip with the wrapping component is a good idea and might work as a hacky workaround but it's still a misbehavior in the library. You can't expect that all users are noticing it and implement the hook in it that way. 😉
Every time the interval is executed or the window gets focused or blurred the entire app re-renders. I fixed the hook but don't have any time to create a PR. If you find any issues in my code, please leave a comment. With my code you can also remove 2 dependencies.
Feel free to use it: