0

I'm using a semantic visibility but I have this error I don't know what I'll pass to my function parameter

could someone help me how could i fix this?

const handleOverlayRef = (c) => {
  console.log(c);
  if (!overlayRect) {
    setOverlayRect({ overlayRect: _.pick(c.getBoundingClientRect(), 'height', 'width') })
  }
}
      <Visibility
        offset={80}
        once={false}
        onTopPassed={()=> stickOverlay()}
        onTopVisible={()=>unStickOverlay()}
        style={overlayFixed ? { ...overlayStyle, ...overlayRect } : {}}
      />

      <div ref={() => handleOverlayRef()} style={overlayFixed ? fixedOverlayStyle : overlayStyle}>
        <Menu
          icon='labeled'
          style={overlayFixed ? fixedOverlayMenuStyle : overlayMenuStyle}
          vertical
        >
          <Menu.Item>
            <Icon name='twitter' />
            Twitter
          </Menu.Item>

          <Menu.Item>
            <Icon name='facebook' />
            Share
          </Menu.Item>

          <Menu.Item>
            <Icon name='mail' />
            Email
          </Menu.Item>
        </Menu>
      </div>

error:

TypeError: c.getBoundingClientRect is not a function

Spirit Yang
  • 97
  • 1
  • 1
  • 8
  • Seee https://stackoverflow.com/questions/51963089/react-element-getboundingclientrect-is-not-a-function – B. Go Dec 13 '19 at 20:43

2 Answers2

1

When we work with ref we should declare it first, like so:

    function Component = props => { 
           const myRef = React.useRef(); 

        const myMethod = event => {
           const {height, width, x, y} = myRef.current.getBoundingClientRect();
           console.log(height, width, x, y);
         }

        return (
       <div ref={myRef} style={overlayFixed ? fixedOverlayStyle : overlayStyle}>


       </div>
     )
 }
Gaspar Teixeira
  • 1,237
  • 11
  • 21
0

Please try to use this.

<div ref={handleOverlayRef} style={overlayFixed ? fixedOverlayStyle : overlayStyle}>
const handleOverlayRef = (c) => {
  console.log(c);
  if (!overlayRect && c) {
    setOverlayRect({ overlayRect: _.pick(c.getBoundingClientRect(), 'height', 'width') })
  }
}