Skip to content

Commit

Permalink
fix: memoize options in react (#166)
Browse files Browse the repository at this point in the history
Fix Max update depth exceeded with useAutoAnimate
  • Loading branch information
justin-schroeder committed Nov 3, 2023
2 parents 8e4626a + 3cd8cae commit 8318a37
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/react/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useCallback, RefCallback } from "react"
import { useState, useCallback, RefCallback, useMemo } from "react"
import autoAnimate, {
AutoAnimateOptions,
AutoAnimationPlugin,
Expand All @@ -16,15 +16,16 @@ export function useAutoAnimate<T extends Element>(
const [controller, setController] = useState<
AnimationController | undefined
>()
const memoizedOptions = useMemo(() => options, [])
const element = useCallback(
(node: T) => {
if (node instanceof HTMLElement) {
setController(autoAnimate(node, options))
setController(autoAnimate(node, memoizedOptions))
} else {
setController(undefined)
}
},
[options]
[memoizedOptions]
)
const setEnabled = useCallback(
(enabled: boolean) => {
Expand Down

1 comment on commit 8318a37

@vercel
Copy link

@vercel vercel bot commented on 8318a37 Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.