在组件卸载React.js之前更改CSS和状态

时间:2020-03-29 07:35:28

标签: javascript reactjs

我是新来的反应者,我需要在卸载组件之前更改CSS类,但是我的组件在卸载之前需要新的道具,而css效果没有任何解决方法。请提前

     getNext=()=>{

    this.setState({
        isCardFadeOut: true
    })
    this.secondCard.current.classList.remove('second-card')
    this.secondCard.current.classList.add('main-card')
    this.thirdCard.current.classList.remove('third-card')
    this.thirdCard.current.classList.add('second-card')
    this.props.parent.getNext()

}

1 个答案:

答案 0 :(得分:0)

我使用了onTransitionEnd事件监听器来调用

this.props.parent.getNext()

和getNext()上,我对css进行了更改,其中包括转换,并使函数调用如下所示同步:

 <div ref={this.thirdCard} 
      onTransitionEnd={this.handleGetNext} 
      className="content-card third-card"/>


 handleGetNext = () => {
        this.props.parent.getNext()
    }


 getNext = () => {

        this.firstCard.current.classList.add('card-fading')
        this.secondCard.current.classList.remove('second-card')
        this.secondCard.current.classList.add('main-card')
        this.thirdCard.current.classList.remove('third-card')
        this.thirdCard.current.classList.add('second-card')


    }