[英]Open a modal from a component
I am working on a component where I need to display and hide a modal. 我正在一个需要显示和隐藏模态的组件上工作。
this is what I have in the render method in React 这就是我在React中的render方法中所拥有的
<div style={{visibility : this.state.displayModal}}>
<p>Pop up: Bet Behind Settings</p>
</div>
<button onClick={this._openModal}>CLICK</button>
and here is the function 这是功能
_openModal = () => {
if (this.state.displayModal === 'hidden') {
this.setState({
displayModal : 'visible',
})
} else {
this.setState({
displayModal : 'hidden',
})
}
}
the main concern I have, is, how to set the state in a more elegant way, or this should be the way to do it ? 我主要关心的是如何以更优雅的方式设置状态,或者这应该是这样做的方式?
here the full code 这是完整的代码
constructor (props) {
super(props);
this.state = {
displayModal : 'hidden',
}
}
render () {
return (
<div style={{visibility : this.state.displayModal}}>
<p>Pop up: Bet Behind Settings</p>
</div>
<button onClick={this._openModal}>CLICK</button>
)
}
_openModal = () => {
if (this.state.displayModal === 'hidden') {
this.setState({
displayModal : 'visible',
})
} else {
this.setState({
displayModal : 'hidden',
})
}
}
so, what should be the way to this pop up in a React way. 因此,应该如何以React的方式弹出此窗口。
I think it's a good way to do it. 我认为这是个好方法。 But it will be more concise if you make displayModel a boolean:
但是,如果将displayModel设为布尔值,它将更加简洁:
_toggleModal = () => this.setState({displayModal: !this.state.displayModal})
On a complex page using hidden will be a performance issue. 在复杂的页面上,使用隐藏将是性能问题。 Try something like this instead;
尝试这样的事情;
render() { var returnIt; if (this.state.hide) { returnIt = null; } else { returnIt = ( <div style={{visibility : this.state.displayModal}}> <p>Pop up: Bet Behind Settings</p> </div> <button onClick={this._openModal}>CLICK</button> ) } return (returnIt); }
This is just a personal opinion, but I think a better UX would be that the button should only be used to open the modal; 这只是个人观点,但我认为更好的UX是只应使用按钮打开模式; and the modal should be closed by either clicking the X in the modal (if there is) or when you click anywhere outside the modal.
并且应该通过单击模态中的X(如果有)或在模态之外的任何位置单击来关闭模态。
That said if you definitely need the button to toggle between the 2 states, how about something like this? 这就是说,如果您确实需要在两个状态之间切换的按钮,那么类似的事情呢?
constructor (props) {
super(props);
this.state = {
displayModal : false
}
}
render () {
return (
<div style={{visibility : this.state.displayModal === true ? 'visible' : 'hidden'}}>
<p>Pop up: Bet Behind Settings</p>
</div>
<button onClick={this._toggleModal}>CLICK</button>
)
}
_toggleModal = () => {
const current = this.state.displayModal;
this.setState({
displayModal : !current
});
}
Using https://github.com/fckt/react-layer-stack you can do like so: 使用https://github.com/fckt/react-layer-stack可以这样做:
import { Layer, LayerContext } from 'react-layer-stack'
// ... for each `object` in array of `objects`
const modalId = 'DeleteObjectConfirmation' + objects[rowIndex].id
return (
<Cell {...props}>
// the layer definition. The content will show up in the LayerStackMountPoint when `show(modalId)` be fired in LayerContext
<Layer use={[objects[rowIndex], rowIndex]} id={modalId}> {({
hideMe, // alias for `hide(modalId)`
index } // useful to know to set zIndex, for example
, e) => // access to the arguments (click event data in this example)
<Modal onClick={ hideMe } zIndex={(index + 1) * 1000}>
<ConfirmationDialog
title={ 'Delete' }
message={ "You're about to delete to " + '"' + objects[rowIndex].name + '"' }
confirmButton={ <Button type="primary">DELETE</Button> }
onConfirm={ this.handleDeleteObject.bind(this, objects[rowIndex].name, hideMe) } // hide after confirmation
close={ hideMe } />
</Modal> }
</Layer>
// this is the toggle for Layer with `id === modalId` can be defined everywhere in the components tree
<LayerContext id={ modalId }> {({showMe}) => // showMe is alias for `show(modalId)`
<div style={styles.iconOverlay} onClick={ (e) => showMe(e) }> // additional arguments can be passed (like event)
<Icon type="trash" />
</div> }
</LayerContext>
</Cell>)
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.