5

我有以下代码:

import React from "react";
import { BrowserRouter, Route, NavLink } from "react-router-dom";
import { AnimatePresence, motion } from "framer-motion";
import Hi from "./Hi";
import Something from "./Something";
import "./App.css";

const App = () => {
  return (
    <BrowserRouter>
      <AnimatePresence>
        <NavLink to="/">Hi</NavLink>
        <br />
        <NavLink to="/something">Something</NavLink>
        <hr />
        <Route path="/" exact={true} key="1">
          <motion.div
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
            transition={{ duration: 2 }}
            key="hi"
          >
            <Hi />
          </motion.div>
        </Route>
        <Route path="/something" exact={true} key="2">
          <motion.div
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
            key="something"
            transition={{ duration: 2 }}
          >
            <Something />
          </motion.div>
        </Route>
      </AnimatePresence>
    </BrowserRouter>
  );
};

export default App;

假设它必须在安装 and 时执行淡入动画,并在卸载时淡出安装动画发生,但另一个没有。在动画效果开始之前,这两个组件都已卸载,并且所有动画库都会出现问题:、和其他。<Hi/><Something/>framer-motionreact-transition-group

4

1 回答 1

0

不确定您是否仍在寻找答案,但我通过将exitBeforeEnter标志添加到AnimatePresence.

所以...

//...
<AnimatePresence exitBeforeEnter>
        <NavLink to="/">Hi</NavLink>
        <br />
        <NavLink to="/something">Something</NavLink>
        <hr />
//...
于 2021-05-19T12:29:19.133 回答