在 Material UI Transitions doc 中,有一些例子展示了一个按钮触发过渡效果。我有一个情况是当一个按钮触发状态改变时,我想让旧数据过渡出去,然后新数据过渡进来。我唯一找到的方法是使用
setTimeout
。是否有更好的方法?
在 CodeSandbox 中
import React from "react";
import Slide from "@material-ui/core/Slide";
import Button from "@material-ui/core/Button";
import Typography from "@material-ui/core/Typography";
const words = ["one", "two", "three"];
const transitionDuration = 500;
class TransitionCycle extends React.Component {
state = {
activeIndex: 0,
elementIn: true
};
onClick = () => {
this.setState({
elementIn: false
});
setTimeout(() => {
this.setState({
elementIn: true,
activeIndex: (this.state.activeIndex + 1) % words.length
});
}, transitionDuration);
};
render() {
const { activeIndex, elementIn } = this.state;
return (
<div>
<Button onClick={this.onClick}>Cycle</Button>
<Slide
in={this.state.elementIn}
timeout={transitionDuration}
direction="right"
>
<Typography variant="h1">{words[this.state.activeIndex]}</Typography>
</Slide>
</div>
);
}
}
export default TransitionCycle;
这是在Material UI中实现连续转换的最佳方法吗?使用setTimeout
感觉有些奇怪。