我有一个调用特定状态的函数,但有一条半相同的行。这是我的功能;
DoThis(type) {
if (type === 'a') {
this.setState({ activeA: { display: 'block', opacity: 1 } }, () => setTimeout(() => {
this.setState({ activeA: { opacity: 0 } }, () => setTimeout(() => {
this.setState({ activeA: { display: 'none' } })
}, 300))
}, 2000));
} else if (type === 'b') {
this.setState({ activeB: { display: 'block', opacity: 1 } }, () => setTimeout(() => {
this.setState({ activeB: { opacity: 0 } }, () => setTimeout(() => {
this.setState({ activeB: { display: 'none' } })
}, 300))
}, 2000));
} // ...some other active(n) conditions.
}
它非常混乱,我只是希望它没有拥堵。
这是我的状态:
this.state ={
activeA: { display: 'none', opacity: 0 },
activeB: { display: 'none', opacity: 0 },
// ...some other active(n) states...
}
有什么办法可以解决这类问题?