Reactjs Can';无法获取参考当前偏移网络宽度
我正在尝试制作上下文菜单 我想从ref.current获取offsetWidth和offsetHeight,但console.log打印未定义Reactjs Can';无法获取参考当前偏移网络宽度,reactjs,typescript,Reactjs,Typescript,我正在尝试制作上下文菜单 我想从ref.current获取offsetWidth和offsetHeight,但console.log打印未定义 const ContextMenu:React.FC=props=>{ const thisComponent=useRef(null); const[isVisible,setIsVisible]=useState(false); 让thisComponentHeight=null; 让thisComponentWidth=null; useffect
const ContextMenu:React.FC=props=>{
const thisComponent=useRef(null);
const[isVisible,setIsVisible]=useState(false);
让thisComponentHeight=null;
让thisComponentWidth=null;
useffect(()=>{
文件。添加的列表器(“上下文菜单”,上下文菜单句柄);
if(thisComponent&&thisComponent.current){
thisComponentWidth=thisComponent.current;
thisComponentHeight=thisComponent.current;
console.log(thisComponent.current)
}
}, []);
返回(
测验
测试2
);
};
导出默认上下文菜单;
这是console.log(thisComponent.current)的图片代码>
该列
组件看起来像是属于另一个库,而不是用于反应的本地组件,因此他们可能在当前
对象中定义了自己的属性集。只需将列
包装在div
或span
中,然后将ref
标记给该标记即可。您将能够获得offsetWidth
以及本机DOM属性
return (
<div ref={thisComponent}>
<Column>
<div>test</div>
<div>test2</div>
</Column>
</div>
);
返回(
测验
测试2
);