语义反应ui弹出关闭按钮

yuz*_*zır 5 javascript reactjs semantic-ui semantic-ui-react

我正在使用semantic-react-ui的Popup组件,我想知道如何通过单击弹出窗口内的按钮而不使用jquery来触发关闭弹出事件.

谢谢

Dan*_*ane 9

根据文档,您必须创建一个受控制的Popup.
创建一个嵌套Popup组件的组件,并在其中维护一个状态:

class ControlledPopup extends React.Component {
  constructor() {
    super();
    this.state = {
      isOpen: false
    };                  // state to control the state of popup
  }

  handleOpen = () => {
    this.setState({ isOpen: true });
  }

  handleClose = () => {
    this.setState({ isOpen: false });
  }

  render() {
    return (
      <div>
        <Popup
          trigger={<button>click to open</button>}
          content={<button onClick={this.handleClose}>click to close</button>}
          on='click'
          open={this.state.isOpen}
          onOpen={this.handleOpen}
        />
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我会添加`onClose = {this.handleClose}`以在菜单外单击时启用关闭的默认行为. (4认同)