npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-use-safe-callback

v0.1.0

Published

Hook that returns function that will fire no-op if component is not mounted.

Downloads

1

Readme

react-use-safe-callback

Hook that returns function that will fire no-op if component is not mounted.

Use case and usage

If you had situations where you were doing something async like this:

const [user, setUser] = useState(null);
useEffect(() => {
  fetchUser().then((user) => {
    setUser(user);
  });
}, []);

Issue with this is that if your component happens to be unmounted before the loading of fetchUser is done, you are calling setUser (changing the state) on an unmounted component.

You must likely encountered console error like this: Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application.

To assure that this does not happen, use this hook. It will assure that actual function is called only if component is mounted, otherwise it performs a silent no-op. Previous code example, fixed using this hook would look like:

const [user, setUser] = useState(null);
const safeSetUser = useSafeCallback((user) => setUser(user), []);
useEffect(() => {
  fetchUser().then((user) => {
    safeSetUser(user);
  });
}, []);

Things to have in mind

  • Callback you have provided into hook will be fired only if component is mounted. That means that besides not fireing when/after component is un-mounting, it will not also fire until it has been mounted once, all the returned function will do is a silent no-op.
  • Since there is no guarantee that function will be called when component is mounted and possibly a no-op will be done, the returned value can be undefined (if you use Typescript it will warn you)

Linting

You can add this hook to additionalHooks rule of react-hooks plugin. For example

"react-hooks/exhaustive-deps": ["warn", {
  "additionalHooks": "(useMyCustomHook|useSafeCallback)"
}]

License

This project is licensed under the MIT License. Copy of license can be found in repository root.