Search code examples
reactjsreact-bootstrap

React Bootstrap Popover: Pre-Open Popover on initialization


How do I pre-open (initialize as open) a React Bootstrap Popover? I have the following which works in the initial closed state,

const popoverApproverNewAgreement = (
    <Popover id="popover-approverNewAgreement">
        <Popover.Title as="h3"></Popover.Title>
        <Popover.Content>
        Some text
        </Popover.Content>
    </Popover>
);

<OverlayTrigger trigger="click" rootClose placement="bottom" overlay={popoverApproverNewAgreement}>
    <a href="javascript:void(0)" className="more-info">
        <i className="fas fa-question-circle" aria-label="more information">
        </i>
    </a>
</OverlayTrigger>  

Solution

  • I couldn't get it to work with OverlayTrigger but succeeded with the more generic Overlay which takes a show={..} so that you can show/hide with a state variable.

    Overlay requires a target ref as below.

    // Ref for Link or Button triggering overlay (required for <Overlay> usage)
    const refLink = useRef(null); 
    
    // State variable
    const [overlayOpen, setOverlayOpen]  = useState(true); // Initially open
    
    <a ref={refLink} href="javascript:void(0)" onClick={() => setOverlayOpen(!overlayOpen)}>
        <i className="fas fa-question-circle" aria-label="more information">
        </i>
    </a>
    <Overlay target={refLink.current} show={overlayOpen} 
             onHide={() => setOverlayOpen(false)} rootClose placement="bottom">
        {popoverApprover}
    </Overlay>