如何在悬停时使用reactstrap popover?

时间:2019-07-09 08:28:11

标签: javascript reactjs popover reactstrap

我是新来的反应者。在这里,我使用的是一个名为reactstrap的库,其中,

import React from 'react';
import { Button, Popover, PopoverHeader, PopoverBody } from 'reactstrap';

export default class Example extends React.Component {
  constructor(props) {
    super(props);

    this.toggle = this.toggle.bind(this);
    this.state = {
      popoverOpen: false
    };
  }

  toggle() {
    this.setState({
      popoverOpen: !this.state.popoverOpen
    });
  }

  render() {
    return (
      <div>
        <Button id="Popover1" type="button">
          Launch Popover
        </Button>
        <Popover placement="bottom" isOpen={this.state.popoverOpen} target="Popover1" toggle={this.toggle}>
          <PopoverHeader>Popover Title</PopoverHeader>
          <PopoverBody>Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.</PopoverBody>
        </Popover>
      </div>
    );
  }
}

现在,我要尝试的是当用户hover在该按钮本身上时,只有该用户才能看到该popover。所以有什么办法可以在悬停本身上使用它?

1 个答案:

答案 0 :(得分:1)

您可以为此使用onMouseEnteronMouseLeave

<Button
   id="Popover1"
   type="button"
   onMouseEnter={this.onHover}
   onMouseLeave={this.onHoverLeave}
>
    Launch Popove
</Button>

函数应该是

onHover = () => {
  this.setState({
    popoverOpen: true,
  })
}

onHoverLeave = () => {
  this.setState({
    popoverOpen: false,
  })
}

Demo