reactjs - c.getBoundingClientRect 不是函数
问题描述
我正在使用语义可见性但我有这个错误我不知道我将传递给我的函数参数
有人可以帮我解决这个问题吗?
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>
错误:
TypeError:c.getBoundingClientRect 不是函数
解决方案
当我们使用 ref 时,我们应该首先声明它,如下所示:
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>
)
}
推荐阅读
- angular - 如何有条件地从生产构建中排除角度组件?
- javascript - 如何从添加的 iframe 中删除或隐藏 div
- android - 在我的荣誉手机中单击消息中的自动填充后,在 Android Studio 中使用 Firebase 进行手机身份验证时键盘未弹出
- javascript - express.js 中的 Next(err) 处理程序消息
- html - 如何在 Vue 中使用 data-image-src="../assets/images/about.jpg"
- javascript - 如何包装div的内容
- javascript - 逗号分隔的正则表达式,如何用html标签包装单词并提取数字
- r - 重新排序 R 图中的标签
- selectize.js - 在 Selectize.js 上设置大小
- python - vs代码中的库导入问题