Exit Property not working in Framer Motion with react js

huangapple go评论68阅读模式
英文:

Exit Property not working in Framer Motion with react js

问题

以下是代码的翻译部分:

Framer Motion中的Exit Property不起作用,使用React JS一切都正常,但退出动画无效,我已经尝试了一个星期,请帮帮我!

我已安装了最新版本的Framer Motion "framer-motion": "^10.10.0",但退出属性仍然没有响应。

import React, { useEffect, useState, useRef } from 'react';
import { Box, Grid, CardContent, CardActionArea, CardMedia, Typography, styled, List, ListItem, createStyles, Card, TextField, Link, Button, TextareaAutosize } from '@mui/material';
import { motion, useScroll, AnimatePresence } from 'framer-motion';
import '../Style/app.css';
import axios from 'axios';
import CancelIcon from '@mui/icons-material/Cancel';

export default function Carosel({ handleClose }) {
  const [post, setpost] = useState([]);

  const getload = async () => {
    const response = await axios.get('http://127.0.0.1:8000/media/carosel');
    console.log(response.data);
    console.log(post);
    setpost(response.data);
    console.log(post);
  }

  useEffect(() => {
    getload();
  }, [])

  const dropIn = {
    hidden: {
      y: "100vh",
      opacity: 0,
    },
    visible: {
      y: "0",
      opacity: 1,
      transition: {
        duration: 1.5,
        type: "spring",
        damping: 35,
        stiffness: 100,
      },
    },
    exit: {
      y: "-100vh",
      opacity: 0,
      transition: {
        duration: 1.5,
        type: "spring",
        damping: 35,
        stiffness: 100,
      },
    },
  };

  return (
    <>
      <AnimatePresence mode='wait'>
        <Box
          component={motion.div}
          variants={dropIn}
          initial="hidden"
          animate="visible"
          exit="exit"
          onClick={(e) => e.stopPropagation()}
        >
          <Button>
            <CancelIcon />
          </Button>
          {post.map(elem => {
            return (
              <Box>
                <Box
                  component='img'
                  width="100%"
                  height="100%"
                  src={elem.Image}
                ></Box>
                <Typography>
                  {elem.Title}
                </Typography>
              </Box>
            )
          })}
        </Box>
      </AnimatePresence>
    </>
  )
}
英文:

Exit Property not working in Framer Motion with react js Everything is going well but exit motion is not working i am trying this for more than a week please help me!

Exit Property not working in Framer Motion with react js Everything is going well but exit motion is not working i am trying this for more than a week please help me!I have installed latest version of framer motion "framer-motion": "^10.10.0", But also no response on exit property.

import React ,{useEffect , useState ,useRef } from &#39;react&#39;;
import { Box, Grid,CardContent ,CardActionArea,CardMedia, Typography, styled, List, ListItem, createStyles, Card, TextField, Link, Button, TextareaAutosize } from &#39;@mui/material&#39;;
import {motion , useScroll,AnimatePresence } from &#39;framer-motion&#39;
import &#39;../Style/app.css&#39;
import axios from &#39;axios&#39; 
import CancelIcon from &#39;@mui/icons-material/Cancel&#39;;
export default function Carosel({handleClose}) {
const [post,setpost] = useState([]);
const getload = async()=&gt;{
const response = await axios.get(&#39;http://127.0.0.1:8000/media/carosel&#39;);
console.log(response. Data);
console.log(post);
setpost(response.data);
console.log(post);
}
useEffect(()=&gt;{
getload();
},[])
const dropIn ={
hidden:{
y:&quot;100vh&quot;,
opacity:0,
},
visible:{
y:&quot;0&quot;,
opacity:1,
transition:{
duration:1.5,
type:&quot;spring&quot;,
damping:35,
stiffness:100,
},
},
exit:{
y:&quot;-100vh&quot;,
opacity:0,
transition:{
duration:1.5,
type:&quot;spring&quot;,
damping:35,
stiffness:100,
},
},
};
return (
&lt;&gt;
&lt;AnimatePresence mode=&#39;wait&#39;&gt;
&lt;Box
component={motion.div}
variants={dropIn}
initial=&quot;hidden&quot;
animate=&quot;visible&quot;
exit=&quot;exit&quot;
onClick={(e)=&gt; e.stopPropagation()}
&gt;
&lt;Button&gt;
&lt;CancelIcon /&gt;
&lt;/Button&gt;
{post. Map(elem)=&gt;{
return(
&lt;Box&gt;
&lt;Box
component=&#39;img&#39;
width=&quot;100%&quot;
height=&quot;100%&quot;
src={elem.Image}
&gt;
&lt;/Box&gt;
&lt;Typography&gt;
{elem.Title}
&lt;/Typography&gt;
&lt;/Box&gt;
)
})
}
&lt;/Box&gt;
&lt;/AnimatePresence&gt;    
&lt;/&gt;
)
}

答案1

得分: 1

文档中:

直接子元素必须分别具有唯一的 key 属性,以便 AnimatePresence 可以跟踪它们在树中的存在。

如果您为主要的 Box 组件添加一个 key 属性,它应该正常工作。

英文:

From the docs:
> Direct children must each have a unique key prop so AnimatePresence can track their presence in the tree.

If you add a key prop to your main Box component it should work.

huangapple
  • 本文由 发表于 2023年4月11日 16:41:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定