Cleanup
Each preset effect provided by this library, as well as the createMotion function, return a cleanup function. This cleanup function is designed to manage the lifecycle of the motion effect, ensuring that any resources used by the effect are properly released when the effect is no longer needed.
const myEffect = createMotion(myMotionEffect, myMotionParams);
// ... some other amazing things happening meanwhile ...
myEffect(); // Invoke the cleanup function when the effect is no longer needed
In this example, myEffect is the cleanup function returned by createMotion
. When you're done
with the motion effect, you simply invoke myEffect()
to clean up.
The cleanup function is returned as part of the MotionDestroy (opens in a new tab) interface, which is a function that takes no arguments and returns no value. When invoked, this function will perform any necessary cleanup operations, such as unsubscribing from observables or releasing other resources.
This pattern of returning a cleanup function is common in libraries that manage resources (React, Vue, others), as it provides a clear and explicit way for the user of the library to control when resources are released.