简体   繁体   English

如何修复无法读取 null 的属性“clientWidth”?

[英]How to fix Cannot read property 'clientWidth' of null?

I am getting an error "Cannot read property 'clientWidth' of null" when I am resizing the window.调整窗口大小时出现错误“无法读取 null 的属性‘clientWidth’”。 I think, this is because, I am getting ref null.我想,这是因为,我得到 ref null。 Any idea how can I fix it?知道我该如何解决吗?

import React, { useRef, useState, useEffect } from "react";

const data = [
  "Some short text",
  "Some text that is a little bit longer",
  "Some text that will need to wrap but still fits on two lines",
  "A massive range of hammer drill machines and rotary hammers for SDS-plus accessory tools."
];
const TooltipDiv = props => {
  const divRef = useRef(null);
  const [allowTooltip, setAllowTooltip] = useState(false);
  useEffect(() => {
    if (!tooltip && divRef.current.scrollWidth > divRef.current.clientWidth) {
      setTooltip(true);
    } else if (window.addEventListener) {
      window.addEventListener('resize', () => {
        if (!tooltip && divRef.current.scrollWidth > divRef.current.clientWidth) {
            setTooltip(true);
        }
      })
    }
  }, [tooltip, divRef]);
  if (allowTooltip) {
    return (
      <Tooltip title={props.text}>
        <div ref={divRef} className={props.className}>
          {props.text}
        </div>
      </Tooltip>
    );
  }
  return (
    <div ref={divRef} className={props.className}>
      {props.text}
    </div>
  );
};
function App(props) {
  return (
    <>
      {data.map(text => {
        return (
          <>
            <TooltipDiv text={text} className={props.classes.listItem} />
          </>
        );
      })}
    </>
  );
}

I am getting an error "Cannot read property 'clientWidth' of null" when I am resizing the window.调整窗口大小时出现错误“无法读取 null 的属性‘clientWidth’”。 I think, this is because, I am getting ref null.我想,这是因为,我得到 ref null。 Any idea how can I fix it?知道我该如何解决吗?

the window.addEventListener is trying to read a value that doesn't exist yet. window.addEventListener正在尝试读取尚不存在的值。 At this moment, divRef is null.此时 divRef 为空。

I think you should try to test the existence of div.current and then, perform your if statement:我认为您应该尝试测试 div.current 的存在,然后执行您的if语句:

if(tooltip && divRef.current && divRef.current.scrollWidth && divRef.current.clientWidth){
  //your statement
}

also, try to perform the operation without [divRef, tooltip] because you want it to execute only once ( [] ), I suppose另外,尝试在没有[divRef, tooltip]情况下执行操作[divRef, tooltip]因为您希望它只执行一次 ( [] ),我想

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java脚本错误“无法读取null的属性&#39;clientWidth&#39;为空” - Java Script error “Cannot read property 'clientWidth' of null” with modal 我得到以下JS代码:无法读取null的属性&#39;clientWidth&#39; - I get the following JS code: Cannot read property 'clientWidth' of null 如何修复无法读取 null 的属性“forEach” - How to fix Cannot read property 'forEach' of null 如何修复“无法读取‘null’的属性‘id’” - How to fix 'Cannot read property 'id' of 'null' 如何修复“无法读取 null 的属性‘addEventListener’” - How to fix “Cannot read property 'addEventListener' of null” TypeError:无法读取未定义的属性“clientWidth” - TypeError: Cannot read property 'clientWidth' of undefined 使用 React 的自定义鼠标光标:未捕获的类型错误:无法读取 null 的属性“clientWidth” - Custom Mouse Cursor using React: Uncaught TypeError: Cannot read property 'clientWidth' of null 如何使用 Javascript 中的输入修复“无法读取 null 的属性‘值’” - How to fix 'Cannot read property 'value' of null' with an input in Javascript 如何修复未捕获的 TypeError:无法读取 null 的属性“outerHTML” - How to fix uncaught TypeError: Cannot read property 'outerHTML' of null 如何解决:TypeError:无法在我的代码中读取null的属性“ join”? - How to fix: TypeError: Cannot read property 'join' of null on my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM