单击按钮打开模式 [英] opening a modal with the click of a button
问题描述
接下来的代码使用了一个 Modal react 组件:
导出类 AddWorkLogEditor 扩展 React.Component {构造函数(道具){超级(道具);this.addWorkLog = this.addWorkLog.bind(this);this.onOpenModal = this.onOpenModal.bind(this);this.onCloseModal = this.onCloseModal.bind(this);这个.state = {打开:真};}onOpenModal() {this.setState({open: this.props.openModal});}onCloseModal() {this.setState({open:false});}addWorkLog() {}使成为() {常量 bstyle = {背景颜色:'绿色',文本对齐:左",paddingLeft: '0px',白颜色'};常量 {open} = this.state;返回 (<Modal open={open} onClose={this.onCloseModal} little><h3>hi gi</h3><按钮 bsStyle="success" bsSize="small" onClick ={(ev) =>{console.log(ev)} }>保存</按钮></模态></div>);}}我正在尝试使用:
addWorkLog(){返回 <AddWorkLogEditor/>;}
和
createAddWorkLogButton () {返回 (<button style={ { color: '#007a86'} } onClick={this.addWorkLog} >添加工作日志</button>);}
我的意思是,单击此按钮后,什么都没有显示.还有另一种方式来调用该模态吗?我正在从以下位置导入模态:
从react-responsive-modal"导入模态
解决方案 您试图仅在单击按钮后才呈现模式,虽然这对于非反应环境来说是很自然的,但在反应中它以不同的方式工作.在最简单的解决方案中,应该始终呈现 Modal
,并且当用户单击按钮时,您将 modal open
属性更改为 true
.
{/* 你页面的所有标记 */}<按钮点击={() =>this.setState({showModal: true})}>添加工作日志</button>{/* 还要别的吗 */}{/* modal 在这里,但它被隐藏了 */}<Modal open={this.state.showModal}>...</Modal>
或者,您可以完全跳过模态渲染,直到 showModal
变为 true.
this.state.showModal &&<Modal open>...</Modal>
The next code uses a Modal react component:
export class AddWorkLogEditor extends React.Component {
constructor(props) {
super(props);
this.addWorkLog = this.addWorkLog.bind(this);
this.onOpenModal = this.onOpenModal.bind(this);
this.onCloseModal = this.onCloseModal.bind(this);
this.state = {
open:true
};
}
onOpenModal() {
this.setState({open: this.props.openModal});
}
onCloseModal() {
this.setState({open:false});
}
addWorkLog() {
}
render() {
const bstyle = {
backgroundColor: 'green',
textAlign:"left",
paddingLeft: '0px',
color: 'white'
};
const {open} = this.state;
return (
<div>
<Modal open={open} onClose={this.onCloseModal} little>
<h3>hi gi</h3>
<Button bsStyle="success" bsSize="small" onClick ={(ev) => {console.log(ev)} }> Save </Button>
</Modal>
</div>
);
}
}
I am trying to call it using:
addWorkLog()
{
return <AddWorkLogEditor/>;
}
and
createAddWorkLogButton () {
return (
<button style={ { color: '#007a86'} } onClick={this.addWorkLog} >Add Work Log</button>
);
}
I mean, after I click at this button nothing shows up. Is there another way to call that modal? I am importing the modal from:
import Modal from 'react-responsive-modal'
解决方案 You are trying to render the modal only once the button is clicked, while that's quite natural for non-react environments, in react it works in a different way. In the simplest solution the Modal
should be always rendered, and when a user clicks the button you change the modal open
property to true
.
{ /* all the markup of your page */ }
<button onClick={() => this.setState({showModal: true})}>Add Work Log</button>
{ /* anything else */ }
{ /* modal is here but it is hidden */ }
<Modal open={this.state.showModal}>...</Modal>
Alternatively, you can just skip the modal rendering at all until the showModal
becomes true.
this.state.showModal && <Modal open>...</Modal>
这篇关于单击按钮打开模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文