2017-12-19 20 views
1

componentDidMountライフサイクルメソッドでクリックイベントを持つコンポーネントがあります。コンポーネントのアップデート時にDOM要素をクリック

コンポーネントのレンダリングがトリガされるたびにdivをクリックする必要があります。

私の問題は、componentDidMountが1回だけ発生し、コンポーネントが再レンダリングされたときに、クリックイベントが発生しないことです。

クリックイベントが機能する他のライフサイクルメソッドが見つかりませんでした。

これは他の方法でも可能ですか?

クリック方法:

componentDidMount() { 
    this.setState({ 
     audioPlayer: ReactDOM.findDOMNode(this.refs.audio) 
    },() => { 
     this.state.audioPlayer.ontimeupdate =() => { this.timeUpdated() }; 
     this.state.audioPlayer.onprogress =() => { this.progressUpdated() }; 
     if(this.props.playing) { 
     this.divElement.click(); 
     } 
    }); 
    } 

参照のdiv:

<div className='player__control__icons--play'> 
    <div ref={div => this.divElement = div} className='player__control__icon' onClick={this.togglePlay.bind(this)}> 
     <Play /> 
    </div> 
    {skipButtons} 
</div> 
+0

https://reactjs.org/docs/react-component .html#componentdidupdateそのメソッドは、あなたが欲しいものでなければなりません – Jayce444

答えて

0

に私は(最初の1つのカントー約100%確実ではない)componentWillReceivePropscomponentDidUpdateの両方がアクセスでき、参考文献を考えます。 componentWillReceivePropsは、支柱が実際に変更されたかどうかを簡単に確認することができます。追記としても

https://developmentarc.gitbooks.io/react-indepth/content/life_cycle/update/component_will_receive_props.html

componentWillReceiveProps (nextProps) { 
    if (!this.props.playing && nextProps.playing) { 
    this.divElement.click(); 
    } 
} 

あなたが本当にネイティブクリックイベントに依存しない限り、あなたのケースthis.togglePlayには、直接クリックハンドラを呼び出すために、通常はクリーナーです。繰り返しますが、私はこの関数の実装を知らないので、何らかの理由でclickイベントが必要な場合がありますが、ほとんどの場合、実際には必要ではありません:)

+0

ありがとうございました!あなたのコメントは本当に私が問題を解決するのに役立った! – saluab

関連する問題