2017-05-04 51 views
0

我正在對少數一些GSAP動畫組件作出反應,當他們進入和離開DOM。我目前遇到的問題是,只有第一個組件似乎是射擊輸入動畫。第一個組件不會觸發離開動畫,其餘組件不會觸發任何動畫。過渡小組不點火生命週期方法

相關的代碼下面

class Home extends React.Component { 
    render() { 
    const { lastActive, activeSection } = this.state; 

    let fullSection = null; 
    switch(activeSection) { 
     case 1: 
     fullSection = <SlideOne enterFromTwo={lastActive === 2 && activeSection === 1} /> 
     break; 

     case 2: 
     fullSection = <SlideFour /> 
     break; 

     default: 
     return false; 
    } 

    return (
     <TransitionGroup> 
     {fullSection} 
     </TransitionGroup> 
    ) 
    } 
} 

現在的幻燈片一個

class SlideOne extends Component { 

    componentWillEnter() { 
    console.log('component will enter'); 
    } 

    componentWillAppear(callback) { 
    console.log('component will appear'); 

    if (!this.props.enterFromTwo) { 
     this.addAnimation(this.enterAnimation, {callback: callback}); 
    } else { 
     callback(); 
    } 
    } 

    componentWillLeave(callback) { 
    console.log('component will leave'); 
    callback(); 
    } 

    enterAnimation({target, options}) { 
    const title = target.find({name: 'title'}); 
    const tl = new TimelineMax({onComplete: options.callback}); 

    return tl.from(title, 3, { 
     autoAlpha: 0, 
     ease: Power2.easeIn 
    }); 
    } 


    render() { 
    return (
     <FullSection> 
     <BorderedTitle name="title">connecting the music industry</BorderedTitle> 
     </FullSection> 
    ); 
    } 
} 

當幻燈片一個從DOM組件將離開生命週期方法不會被調用刪除。

沒有人有任何瞭解爲什麼這可能是這種情況。

+0

你沒有運行componentWillEnter回調,這可能是它。值得一試。可能值得一個jsfiddle或codepen項目,以便人們可以看到發生了什麼。 –

回答

1

你需要確保任何反應要顯示元素具有獨特的關鍵,即使它是一個單一的項目。

例如

<SlideOne key={ somethingUnique }/><SlideFour key={ somethingUnique }/> - 只要唯一鍵是穩定的。

我似乎忽略了這個時候我只是關心低級API https://facebook.github.io/react/docs/animation.html#low-level-api-reacttransitiongroup

陣營文檔:

注: 必須爲ReactCSSTransitionGroup的所有兒童提供關鍵屬性,即使只渲染一個物品。這就是React如何確定哪些孩子進入,離開或留下的。

https://facebook.github.io/react/docs/animation.html