【发布时间】:2018-12-17 01:11:26
【问题描述】:
在 Material UI Transitions doc 中,有一些按钮触发转换的示例。我有一个按钮触发状态更改的情况,我希望以前的数据转出,然后转入新数据。我发现这样做的唯一方法是使用setTimeout。有没有更好的办法?
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感觉怪怪的。
【问题讨论】:
标签: javascript material-ui transition