gpt4 book ai didi

javascript - react : How to animate the change of the component rendered?

转载 作者:行者123 更新时间:2023-11-29 15:23:20 24 4
gpt4 key购买 nike

我更改通过时间间隔呈现的组件。

我希望每次发生变化时都能添加动画。最好的方法是什么?

constructor (props) {
super(props)
this.state = { currentComponent: 1,
numberOfComponents: 2}
}

componentWillMount() {
setInterval(() => {
if(this.state.currentComponent === 2) {
this.setState({currentComponent: 1})
} else {
this.setState({currentComponent: this.state.currentComponent + 1})
}
}, 5000)
}

render(){

let currentComponent = null;

if(this.state.currentComponent === 1) {
currentComponent = <ComponentOne/>;

} else {
currentComponent = <ComponentTwo/>;
}

return(
currentComponent
)
}

编辑:

同样在尝试使用“react-addons-css-transition-group”时我收到以下错误:

enter image description here

最佳答案

您可以像 section 中提供的那样使用 ReactCSSTransitionGroup。

你的CSS:

.example-enter {
opacity: 0.01;
}

.example-enter.example-enter-active {
opacity: 1;
transition: opacity 500ms ease-in;
}

.example-leave {
opacity: 1;
}

.example-leave.example-leave-active {
opacity: 0.01;
transition: opacity 300ms ease-in;
}

看起来像这样:

import ReactCSSTransitionGroup from 'react-addons-css-transition-group';



class MyComponent extends Component {
constructor (props) {
super(props)
this.state = { currentComponent: 1,
numberOfComponents: 2}
}

componentWillMount() {
setInterval(() => {
if(this.state.currentComponent === 2) {
this.setState({currentComponent: 1})
} else {
this.setState({currentComponent: this.state.currentComponent + 1})
}
}, 5000)
}

render(){

let currentComponent = null;

if(this.state.currentComponent === 1) {
currentComponent = <ComponentOne/>;

} else {
currentComponent = <ComponentTwo/>;
}

return(
<ReactCSSTransitionGroup
transitionName="example"
transitionEnterTimeout={500}
transitionLeaveTimeout={300}>
{currentComponent}
</ReactCSSTransitionGroup>

)
}
}

关于javascript - react : How to animate the change of the component rendered?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41472769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com