简体   繁体   English

UseEffect 不是由依赖关系触发的?

[英]UseEffect not triggered by dependencies?

Someone can help me?有人可以帮助我吗? I'd be really thankful :) The problem here is that when the width and height changes, the UseEffect doesn't run again.我真的很感激:) 这里的问题是,当宽度和高度发生变化时,UseEffect 不会再次运行。 This is a textarea that is being resized...这是一个正在调整大小的文本区域...

const [elem, setElem] = useState<HTMLElement>(document.createElement('textarea'))
const width = window.getComputedStyle(elem, null).getPropertyValue("width");
const height = window.getComputedStyle(elem, null).getPropertyValue("height");

   useEffect(() => {
      setElem(document.getElementById("email") as HTMLElement) 
      console.log(width, height)
   },[elem, width, height])

set the width and height using the useState .使用useState设置宽度和高度。

const [width, setWidth] = useState(undefined);
const [height, setHeight] = useState(undefined);


useEffect(() => {
  const getWidth = () => window.getComputedStyle(elem, null).getPropertyValue("width");

  const getHeight = () => window.getComputedStyle(elem, null).getPropertyValue("height");
  const resizeListener = () => {
      setWidth(getWidth())
      setHeight(getHeight())
    };

  window.addEventListener('resize', resizeListener);

  resizeListener();
  // Don't foget cleanup!
  return () => window.removeEventListener('resize', resizeListener);
  // There's a dependency on the element, so if that changes, re-run the effect
}, [elem])


Attach the resize event listener and update width and height using setWidth and setHeight.附加调整大小事件侦听器并使用 setWidth 和 setHeight 更新宽度和高度。

@William-wang has the right idea. @William-wang 有正确的想法。 Your code wouldn't retrigger the effect because the values never change except potentially when the code re-renders for other reasons.您的代码不会重新触发效果,因为这些值永远不会改变,除非代码因其他原因重新呈现。

While his method using a resize listener on the window will update the width and height, it only happens when the window's size changes.虽然他在窗口上使用调整大小侦听器的方法会更新宽度和高度,但只有在窗口大小发生变化时才会发生这种情况。

I've modified the code to use the newer Resize Observer api .我修改了代码以使用较新的Resize Observer api More details in How to detect DIV's dimension changed?如何检测 DIV 的维度已更改中的更多详细信息 . .

This way, it's possible to get accurate and up-to-date size changes.这样,就有可能获得准确和最新的尺寸变化。 The only issue with this approach is that older browsers don't support it .这种方法的唯一问题是旧浏览器不支持它 Though it does seem to be supported by all modern main browsers at this point.尽管目前所有现代主要浏览器似乎都支持它。

 const { useState, useEffect, useRef } = React; function Example() { const elemRef = useRef(null); const [width, setWidth] = useState(undefined); const [height, setHeight] = useState(undefined); useEffect(() => { if (!elemRef.current) { return; } const resizeListener = () => { setWidth(elemRef.current.offsetWidth); setHeight(elemRef.current.offsetHeight); }; const observer = new ResizeObserver(resizeListener); observer.observe(elemRef.current); // Don't foget cleanup! return () => observer.disconnect(); }, []); return ( <div> width: {width} <br /> height: {height} <br /> <textarea ref={elemRef} /> </div> ); } ReactDOM.render(<Example />, document.getElementById('root'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script> <div id="root"/>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM