Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup function for react #41

Closed
abdoseadaa opened this issue Aug 28, 2024 · 8 comments
Closed

cleanup function for react #41

abdoseadaa opened this issue Aug 28, 2024 · 8 comments

Comments

@abdoseadaa
Copy link

clean up function for useEffect

  • as shown in video below this issue happens when navigating back to page where swapy is defined
  • the more i navigate the more it becomes slow and laggy 💀 , i had to reload window in order to returns back to normal

this is my code version

  useEffect(() => {
    const container = document.querySelector('.data-dashboard-swapy-container')!
    const swapy = createSwapy(container , {
      animation : "none",
    })
    swapy.onSwap(({ data }) => {
      saveSlot("dashboard" , data.object as unknown as ISlotData)
    })


    return () => {
      swapy.enable(true)
    }
    
  }, [])

should have

  useEffect(() => {
    const container = document.querySelector('.data-dashboard-swapy-container');
    if (!container) return;
    const swapy = createSwapy(container, {
      animation: 'dynamic',
    });
    const handleSwap = ({ data }) => {
      saveSlot('dashboard', data.object as unknown as ISlotData);
    };
    swapy.onSwap(handleSwap);
    return () => {
      // need to add this 👇
      swapy.offSwap(handleSwap); // @important
    };
  }, []);
Gabster.And.5.More.Pages.-.Personal.2.comp.-.Microsoft.Edge.2024-08-28.06-18-02.mp4
@Hatchibombotar
Copy link

This is a very annoying issue for developing in auto refreshing environments that only refresh the modified components. I don't understand your example though @abdoseadaa? Should there not be a function that cleans up the whole swapy object rather than just removing an event listener function? e.g.

useEffect(() => {
    ...
    return () => {
      swapy.cleanup()
    };
}

Even without the use of swapy.onSwap, it still gets slow over time.

@abdoseadaa
Copy link
Author

removing listeners or removing all swapy instance anyone will do

   return () => {
      // need to add this 👇
      swapy.offSwap(handleSwap); // @important
    };

in the above example i put this part to show the owner what a cleanup should look like
your example is good too.
for now i replaced swapy with @dnd-kit/core as it is more stable but has more configurations to do

@TahaSh
Copy link
Owner

TahaSh commented Aug 31, 2024

@abdoseadaa Thank you for opening this issue. It's a critical one; I'll make sure to update it soon. I'll let you know :)

@abdoseadaa
Copy link
Author

@TahaSh thank you so much , Great work btw

@TahaSh
Copy link
Owner

TahaSh commented Sep 9, 2024

@abdoseadaa I've added swapy.destroy in v0.2.0. Let me know if you have any questions!

@abdoseadaa
Copy link
Author

Swapy@2.0.0.mp4

it's now even faster than @dnd-kit/core , i am using swapy as main drag drop lib
@TahaSh thanks for update and contact , great work

ربنا يبارك فيك 💕

@abdoseadaa
Copy link
Author

issue resolved

@TahaSh
Copy link
Owner

TahaSh commented Sep 10, 2024

@abdoseadaa I'm really happy to hear that. Thank you! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants