单击按钮打开模式

2024-01-08

下一个代码使用 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/>;
}

and

 createAddWorkLogButton () {

    return (
        <button style={ { color: '#007a86'} } onClick={this.addWorkLog} >Add Work Log</button>
    );
 }

我的意思是,单击此按钮后什么也没有显示。还有另一种方法来调用该模态吗?我从以下位置导入模态:

从'react-responsive-modal'导入模态


您尝试仅在单击按钮后渲染模式,虽然这对于非反应环境来说是很自然的,但在反应中它以不同的方式工作。在最简单的解决方案中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成为现实。

this.state.showModal && <Modal open>...</Modal>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单击按钮打开模式 的相关文章

随机推荐