您好我正在玩ReactJS,並且發現這個非常棒的模式組件在模態中打開Videoes,但是當我將模態放到一個帶有多個鏈接的循環中並打開模式時,如果我有5個鏈接,它會打開5次。我做錯了什麼?ReactJS Modal在循環中打開多次
模態分量:https://github.com/appleple/react-modal-video
import React from 'react'
import ReactDOM from 'react-dom'enter code here
import ModalVideo from 'react-modal-video'
class App extends React.Component {
constructor() {
super()
this.state = {
isOpen: false
}
this.openModal = this.openModal.bind(this)
}
openModal() {
this.setState({isOpen: true})
}
render() {
return (
<div>
<ModalVideo channel='youtube' isOpen={this.state.isOpen} videoId='L61p2uyiMSo' />
<button onClick={this.openModal}>Open</button>
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
)
我的循環與模態分量內部:
render(){
return(
<div>
{(this.props.frag.all == null) ? null :
this.props.frag.all.map((frags, i) => {
return (
<li key={frags.id} className="group" id="" style={{width: 'calc(13% - 30px)'}}>
<ModalVideo channel='youtube' isOpen={this.state.isOpen} videoId='{frags.url}' />
<button onClick= {this.openModal.bind(this)}>Open</button>
</li>
)})
}
</div>
剛剛嘗試過你的想法,它的工作完美,它打開和關閉沒有問題!感謝您的解釋,我想我現在幾乎可以了!的xD – DbTheChain