Coder Social home page Coder Social logo

[Feature]: useUnmountEffect about slash HOT 4 CLOSED

toss avatar toss commented on August 18, 2024
[Feature]: useUnmountEffect

from slash.

Comments (4)

HoseungJang avatar HoseungJang commented on August 18, 2024 1

Thanks @ChanhyukPark-Tech .

Could you show us some example?

Because I think there are not much cases to use only cleanup function without any "effect".

I think this is the general case:

useEffect(() => {
  window.addEventListener(/* ... */); // the "effect".
  return () => window.removeEventListener(/* ... */); // cleaning up the "effect".
}, []);

from slash.

raon0211 avatar raon0211 commented on August 18, 2024 1

This is actually an anti-pattern since every effect and cleanup must match. (See https://beta.reactjs.org/learn/synchronizing-with-effects) If not, the app might break in React 18 strict mode.

So I think it is not appropriate for this hook to be merged into our libraries. For debounce and Lottie β€” I think they are incorrect code. So I think we should fix them.

from slash.

ChanhyukPark-Tech avatar ChanhyukPark-Tech commented on August 18, 2024

I agree too much with the opinion that there are not many situations where there will be cleanup without effect!
The case that I need to use this hook is as follows

currnet @toss/lottie: Lottie.tsx 177 line

  useEffect(() => {
    if (!isVisible) {
      return;
    }

    if (!autoPlay) {
      return;
    }

    startAnimation();

    return () => {
      animationChain?.destroy();
    };
  }, [animationChain, autoPlay, isVisible, startAnimation]);

I think animationChain is
It doesn't seem to have much to do with autoPlay, isVisible, and startAnimation in the dependency array.
But I felt awkward being in the useEffect hook where they were connected.

If the animationChain should be destroyed when this Lottie.tsx component is unmounted,

useUnmountEffect(() => animationChain?.destroy());

at the top of this file
I thought it would be better to write that!

I can also use useUnmountEffect if I need to execute the callback I received from the parent component when the child component is unmounted

Another example

In useDebounce hook 28line

useEffect(() => {
    return () => {
      debounced.cancel();
    };
  }, [debounced]);

there is no effect but only cleanup function is executing
So I think useUnmountEffect can improve this situation using

useUnmountEffect(() => debounced.cancle());

thank youπŸ˜ƒ

from slash.

ChanhyukPark-Tech avatar ChanhyukPark-Tech commented on August 18, 2024

Thank you for your kind explanation!
If I am working on a code to use like that pattern in the future, I should think about changing the structure itself.
I learned a lot!

from slash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.