【发布时间】:2021-11-04 05:48:45
【问题描述】:
我们如何使用react-spring 动画显示记录。我们可以在 useEffect() 中调用useTransition 吗?当用户导航到页面时,我想从下到上一一为记录设置动画。我试过下面什么都没有发生..?任何建议都会非常有帮助。
https://codesandbox.io/s/inspiring-snowflake-34dpx?file=/src/App.js:711-881
import { useEffect, useState } from "react";
import { useTransition, animated } from "react-spring";
import "./styles.css";
const array1 = [
{ id: "1", name: "Cat" },
{ id: "2", name: "Dog" },
{ id: "3", name: "Cow" }
];
export default function App() {
const [list, setList] = useState(array1);
const height = 20;
useEffect(() => {
setList(array1);
}, []);
const transitions = useTransition(
Object.entries(list).map((data, i) => ({ ...data, y: i * height })),
{
from: { position: "absolute", opacity: 0 },
enter: { height: 0, opacity: 1 },
leave: { opacity: 0 },
config: { tension: 220, friction: 120 }
}
);
return (
<div className="App">
{/* {Object.entries(list).map((item, index) => (
<div key={item.id} className="record">
<span key={item.name}>{item.name}</span>
</div>
))} */}
{Object.entries(transitions).map(({ item, props, key }) => (
<animated.div key={key} style={{ ...props, position: "absolute" }}>
<div key={item.id} className="record">
<span key={item.name}>{item.name}</span>
</div>
</animated.div>
))}
</div>
);
}
【问题讨论】:
标签: reactjs react-hooks react-spring