Stanko / react-animate-height

Lightweight React component for animating height using CSS transitions. Slide up/down the element, and animate it to any specific height.
https://muffinman.io/react-animate-height
MIT License
756 stars 53 forks source link

Functional component doesn't work as expected #137

Closed Bdragon93 closed 1 year ago

Bdragon93 commented 1 year ago

Code example https://codesandbox.io/s/react-animate-height-animate-height-when-content-changes-forked-5ntso0?file=/src/index.js

Expected behavior

Possible Solution

Steps to Reproduce (for bugs)

  1. Click "add" button.

Your Environment

Additional context

Stanko commented 1 year ago

Hey @Bdragon93, Again, it is not problem with the library. You've ordered your hooks wrong. On the first render setHeight("auto") was called after setFixedHeight() resulting in height always being being auto. That means animation didn't trigger as height didn't change.

You need to reorder your hooks like this:

  useEffect(() => {
    console.log("set height to auto");
    setHeight("auto");
  }, [items]);

  useEffect(() => {
    console.log("mount");
    setFixedHeight();
  }, []);

Here is corrected sandbox: https://codesandbox.io/s/react-animate-height-animate-height-when-content-changes-forked-gunii1?file=%2Fsrc%2Findex.js

Please check the readme and your code more thoroughly before opening issues. You opened two and commented on the third issue and none of those were actual issues.

Hope this helps!

Cheers!