question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature proposal: isLatest()

See original GitHub issue

Hey it’s franciscop from this PR on my work account. While isMounted() works great for requests where you normally go forward/backwards between pages, I think this could also greatly benefit from an isLatest(), isFresh() or similar. For this code:

export default ({ userId }) => {
  const [user, setUser] = useState(false);

  useAsyncEffect(async isMounted => {
    const user = await fetch(`/users/${userId}`);
    if (!isMounted()) return;
    setUser(user);
  }, [userId]);

  return <div>{user ? user.name : "Loading..."}</div>
};

It’ll not try to set the user on an unmounted component, which is already great and fixing so many issues. But, if the userId changes, the component will not remount so there’s effectively a race condition and if the second fetch() finishes before the first one it’ll overwrite the data. If we also had a isLatest (a superset of isMounted) this race condition could be avoided:

export default ({ userId }) => {
  const [user, setUser] = useState(false);

  useAsyncEffect(async (isMounted, isLatest) => {
    const user = await fetch(`/users/${userId}`);
    if (!isLatest()) return;
    setUser(user);
  }, [userId]);

  return <div>{user ? user.name : "Loading..."}</div>
};

If this feature is welcome, there are three options here:

  • Add it as a new parameter: useAsyncEffect(async (isMounted, isLatest) => ...);. The problem is that isLatest() is a superset of isMounted(), so if you are using isLatest() then isMounted() is not needed.
  • Release a major version with a new API and named parameters: useAsyncEffect(async ({ isMounted, isLatest }) => {...});
  • Make it a bit hacky to keep both of the above options available. No need for major release, can work as a parameter. Both of these work: useAsyncEffect(async isMounted => ...) and useAsyncEffect(async ({ isMounted, isLatest }) => ...)

I like the last one slightly more, and you can remove the legacy way in the future when there’s a major release. But it’s up to what you prefer 😄

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
franciscopcommented, Sep 28, 2019

One concern I had wass race conditions if we keep a single global active ref, but a plain variable active like this would do the trick and won’t suffer from a later useEffect overwriting the previous one, so this looks good.

Why not putting it inside useEffect() though? Any thoughts?

export function useAsyncEffect(callback, cleanup, dependencies) {
  const hasCleanup = (typeof cleanup === 'function');

  useEffect(() => {
    let active = true;
    let result;
    const promise = callback(() => active);
    Promise.resolve(promise).then(value => result = value);

    return () => {
      active = false;

      if (hasCleanup) {
        cleanup(result);
      }
    }
  }, hasCleanup ? dependencies : cleanup);  // since cleanup is optional
}

Note: I reviewed my original isMounted(), and it had a bug that I misinterpreted as a different bug.

0reactions
franciscopcommented, Sep 28, 2019

Awesome, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Write a Great Feature Proposal | by Fiona Podrimaj
Basically, describe the feature you are proposing to include in the project. Also, it is always a good idea to ask your designer...
Read more >
Feature Proposals - OpenSimulator
Feature proposals must be created in conjunction with a post to the opensim-dev mailing list - if they are just created here then...
Read more >
My favorite feature proposal template - Daniel Bachhuber
My favorite feature proposal template ... Is it feature adoption, feature satisfaction, or impact on top-level growth, expansion, ...
Read more >
Latest JavaScript features tutorials - W3schools.io
This tutorial covers latest new JavaScript features will release in 2022. ES13 or ES2022 or EcmaScript2022 features .. EcmaScript 2022 tutorials. EcmaScript2022 ...
Read more >
What's New in PHP 7.4 (Features, Deprecations, Speed) - Kinsta
PHP 7.4 is coming with new features, deprecations, and a boost in performance. ... The Spread operator proposal passed with 43 to 1...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found