接下来的代码使用Modal react组件:
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> ); } }
我正在尝试使用以下方式调用它:
addWorkLog() { return <AddWorkLogEditor/>; }
和
createAddWorkLogButton () { return ( <button style={ { color: '#007a86'} } onClick={this.addWorkLog} >Add Work Log</button> ); }
我的意思是,单击此按钮后,没有任何显示。还有另一种方法可以调用该模式吗?我从以下位置导入模式:
从“反应-响应-模态”导入模态
您试图仅在单击按钮后才渲染模态,这对于非反应性环境是很自然的,但在反应中它以不同的方式起作用。在最简单的解决方案中,Modal应始终呈现,当用户单击按钮时,将modal open属性更改为true。
Modal
open
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>
或者,您可以完全跳过模式渲染,直到showModal变为true。
showModal
this.state.showModal && <Modal open>...</Modal>