首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法获取ref.current.offsetWidth

无法获取ref.current.offsetWidth
EN

Stack Overflow用户
提问于 2019-08-12 10:08:56
回答 1查看 1.4K关注 0票数 0

我一直在努力让contextMenu。

我想从ref.current获取offsetWidth和offsetHeight,但console.log打印未定义。

代码语言:javascript
复制
const ContextMenu: React.FC<ContextMenuProps> = props => {
  const thisComponent = useRef(null);
  const [isVisible, setIsVisible] = useState<boolean>(false);
  let thisComponentHeight = null;
  let thisComponentWidth = null;

  useEffect(() => {
    document.addEventListener("contextmenu", contextMenuHandler);
    if (thisComponent && thisComponent.current) {
      thisComponentWidth = thisComponent.current;
      thisComponentHeight = thisComponent.current;
      console.log(thisComponent.current)
    }
  }, []);

  return (
    <Column ref={thisComponent}>
      <div>test</div>
      <div>test2</div>
    </Column>
  );
};

export default ContextMenu;

这是console.log(thisComponent.current);的照片

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-12 10:31:56

Column组件看起来像是属于另一个库,而不是原生的React,因此它们可能在current对象中定义了自己的属性集。只需将Column包装在divspan中,然后为该标记提供ref。您将能够获得offsetWidth以及原生DOM属性。

代码语言:javascript
复制
  return (
    <div ref={thisComponent}>
      <Column>
        <div>test</div>
        <div>test2</div>
      </Column>
    </div>
  );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57454957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档