reactjs - Is there a way to animate items on and off in a map function where the data used updates from a hook? - Stack Overflow

I'm trying to animate a list of components in React using Chakra UI andor Motion that is created

I'm trying to animate a list of components in React using Chakra UI and/or Motion that is created from data provided by a hook and want to have enter and exit animations when an item is added to the data but it seems like when the data gets updated it just overwrites everything instead of animating first. Here's psuedocode of what I currently have that hopefully just contains what's pertinent:

const item = (itemDetails) => {
    const MotionBox = motion(Box)
    return {
        <MotionBox key="itemDetails.key" initial={false} animate={{opacity: 1, scaleY: 1}} exit={{opacity: 0, scaleY: 0}}>
            //Contents of item
        </MotionBox>
    }
const container = () => {
    const data = useDataHook()
    const [displayedData, setDisplayedData] = useState([])
    const initialRender = useRef(true)
    useEffect(() => {
        if (initialRender.current) {
            setDisplayedData(data)
        } elseif (data.length !== displayedData.length) {
            setDisplayedData(data)
        }
    }, [data, displayedData])
    return {
        <Box>
            <AnimatePresence initial={false}>
                {data.map((item, index) => {
                    return (
                        <Item key={index} itemData={displayedData}/>
                    )
                })
            </AnimatePresence>
        </Box>
    }
}

I've tried a number of things, if I don't do initial=false then every time the parent component is rendered everything animates on again making it look like they're flashing (these are in an accordion so don't want them to animate at start, just when added to the data.) But even without any animate in right now it's not animating out unless the list goes to empty because when the data updates it's re-rendering, I assume, before the animation can happen. Unfortunately I don't have the ability to prevent the parent component from being re-rendered when the data isn't updated.

I've tried things like when trying to apply the item via an apply button having it make the call to the server but set a state variable to prevent updating displayedItems until the success was returned successfully and onAnimationComplete on the motion element completed but it still doesn't animate off.

The ideal is having only the new item components animate on by scaling and fading in (ideally pushing things down smoothly but I don't know how to do that yet) and when an item is removed having it animate out by scaling and fading off. With something like this how do I just get the individual items I want to animate on or off to do so instead of all of them or none of them? I can do a comparison to find which items were added or removed but I wasn't able to figure out how to use that to get that specific item to animate off or on.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745672092a4639466.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信