-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
useFollowValue
#3485
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
useFollowValue
#3485
Conversation
Create a new useAnimatedValue hook that extends useSpring's functionality to support any motion transition type (spring, tween, inertia, keyframes). Changes: - Add attachAnimation and animatedValue functions in motion-dom - Add useAnimatedValue hook in framer-motion - Export AnimatedValueOptions type for TypeScript users - Add comprehensive tests for the new hook This addresses the feature request for a generic hook that accepts animation type in options and returns a MotionValue, providing automatic subscription management and type safety for different animation types. Fixes #3322
Reduce code duplication by making useSpring and springValue thin wrappers around the new useAnimatedValue/attachAnimation implementations. This simplifies maintenance and ensures consistent behavior across all animated value hooks. - springValue now calls animatedValue with type: "spring" - attachSpring now calls attachAnimation with type: "spring" - useSpring now calls useAnimatedValue with type: "spring" - Removed ~105 lines of duplicated code
Code Review: useAnimatedValue HookI have reviewed this PR which introduces useAnimatedValue, a generalized hook for creating animated MotionValues with support for all animation types (spring, tween, inertia, keyframes). This addresses issue 3322. Strengths
Critical IssueMissing source in useInsertionEffect dependency array (use-animated-value.ts:77-79). If source changes from a MotionValue to a primitive or vice versa, the effect will not re-run, causing stale subscriptions. Recommendation: Add source to dependencies array Other Issues
Security & PerformanceSecurity: No concerns identified SummaryWell-implemented feature with excellent refactoring approach. The critical dependency array issue should be addressed before merging. Overall, this is a solid PR that adds valuable functionality. |
Rename all related functions for consistency: - useAnimatedValue → useFollowValue - animatedValue → followValue - attachAnimation → attachFollow - AnimatedValueOptions → FollowValueOptions
PR Review:
|
PR Review: useFollowValue HookThis PR introduces useFollowValue, a generalized animation hook that addresses issue #3322 by providing support for multiple animation types (spring, tween, inertia, keyframes) with a unified API. Strengths
Critical Issues1. Memory Leak in Subscription Cleanup (follow-value.ts:102-128)The attach() call on line 102 only registers stopAnimation as cleanup, but when source is a MotionValue, the subscriptions created on lines 116-120 are returned from attachFollow but not from the passive effect cleanup. This may cause memory leaks. Fix: Consolidate cleanup into a single function passed to value.attach() that handles both animation stop and subscription removal. 2. JSON.stringify in Dependency Array (use-follow-value.ts:79)Using JSON.stringify(options) in useInsertionEffect dependencies fails for non-serializable values (functions, driver) and causes unnecessary re-renders. Suggestion: Remove from deps and document that options should be stable/memoized. 3. Race Condition in Animation Events (follow-value.ts:106-112)Animation events are fired in frame.postRender without checking if the animation/value still exists. If cleanup happens before postRender executes, this accesses destroyed state. Fix: Add an isActive flag that's set to false during cleanup. Other Suggestions
Overall AssessmentSolid PR that addresses the feature request well. Main concerns are memory leaks and race conditions that should be fixed before merging. |
Code Review:
|
Closes #3322