代码之家  ›  专栏  ›  技术社区  ›  Aditya Patnaik

您正试图在AnimatePresence中为多个子对象设置动画,但其exitForgeEnter道具设置为true

  •  0
  • Aditya Patnaik  · 技术社区  · 5 年前

    浏览器控制台出错

    You're attempting to animate multiple children within AnimatePresence,
    but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.
    

    Error Image

    App.js(附在BrowserRouter中)

    <Switch>
           <AnimatePresence exitBeforeEnter>
              <Route key="1"  exact path="/" component={Home}/>
              <Route key="2"  exact path="/home" component={Home}/>
              <Route key="3"  exact path="/articles" component={Articles}/>
           </AnimatePresence>
     </Switch>
    

    主页.js/文章.js

    const Home = props =>{
      return(
        <motion.h1 initial={{x:-100}} animate={{x:0}} exit={{x:-100}}>Home/Articles</motion.h1>
      )
    }
    
    export default Articles
    

    有人能解释一下导致错误的原因吗?

    0 回复  |  直到 5 年前
        1
  •  4
  •   Danila    5 年前

    正如docs所说 exitBeforeEnter :

    如果设置为true,AnimatePresence将仅渲染 一个组件 一次。退出组件将在渲染进入组件之前完成其退出动画。

    因此,启用此道具后,您希望它以另一种方式包裹 Switch 具有 AnimatePresence

    <AnimatePresence exitBeforeEnter>
      <Switch location={location} key={location.pathname}>
        <Route exact path="/" component={Home}/>
        <Route exact path="/home" component={Home}/>
        <Route exact path="/articles" component={Articles}/>
      </Switch>
    </AnimatePresence>
    

    请注意,您还需要通过 key 对于 开关