我尝试为数组中的新条目设置动画,因为它们react-spring
在第一次渲染时工作得很好,但在更新时没有动画
这是一个代码沙箱,我在其中以一定间隔重现了该问题:https ://codesandbox.io/s/01672okvpl
import React from "react";
import ReactDOM from "react-dom";
import { Transition, animated, config } from "react-spring";
import "./styles.css";
class App extends React.Component {
state = { fake: ["a", "b", "c", "d", "e", "f"] };
fakeUpdates = () => {
const [head, ...tail] = this.state.fake.reverse();
this.setState({ fake: [...tail, head].reverse() });
};
componentDidMount() {
setInterval(this.fakeUpdates, 2000);
}
componentWillUnmount() {
clearInterval(this.fakeUpdates);
}
render() {
const { fake } = this.state;
return (
<div className="App">
{fake.map((entry, index) => (
<Transition
native
from={{
transform: `translateY(${index === 0 ? "-200%" : "-100%"})`
}}
to={{ transform: "translateY(0)" }}
config={config.slow}
key={index}
>
{styles => <animated.div style={styles}>{entry}</animated.div>}
</Transition>
))}
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
我尝试了相同的结果Spring
。Transition