简体   繁体   English

以百分比设置时如何在对话框中查找div的高度和宽度

[英]How to find height and width of div within dialog when set in percent

I have below dialog where wrapperRefPopup has width in percentage-我有下面的对话框,其中wrapperRefPopup的宽度为百分比-

<BootstrapDialog
        style={{height:'auto', zIndex:500}}        
        aria-labelledby="customized-dialog-title"
        open={true}
      >        
        <IconButton
          aria-label="close"
          onClick={closeModal}
          sx={{
            position: 'absolute',
            right: 8,
            top: -5,
            color: (theme) => theme.palette.grey[500],
          }}
        >
          <CloseIcon fontSize="medium" style={{paddingTop:"0.5rem", paddingRight:'0.2rem', color:'#000'}} />
        </IconButton>
        <Typography component="div" sx={{
          minWidth: '45rem',
          marginLeft: '4rem',
          marginRight: '4rem',
          paddingTop: '1.5rem',
        }}>
    <div>
        
    <svg ref={svgColorCodeRefPopup} style={{width:"-webkit-fill-available"}}>
</svg>
    </div>
      <div
        ref={wrapperRefPopup}
        style={{ width: "100%", height: "12.5rem", marginBottom: "2rem" }}
      >
        
        <svg ref={svgRefPopup} style={{ width: "100%", height: "110%",marginTop:"0%" }}>
          <g className="x-axis" />
          <g className="y-axis" />
        </svg>
      </div>
      </Typography>
      </BootstrapDialog>

For chart purpose , I want to calculate width in useEffect , which I am trying to calculate through-出于图表目的,我想计算 useEffect 中的宽度,我试图通过 -

const svgRefPopup = useRef();
  const svgColorCodeRefPopup=useRef();  
  const wrapperRefPopup = useRef();

  useEffect(() => {  
    const svg = select(svgRefPopup.current);
    const svgColorCode=select(svgColorCodeRefPopup.current);
    
    const { width, height } = wrapperRefPopup.current?.getBoundingClientRect?.()||0;

    ...
    ...

},[]);

When I try to console log width and height , I get undefined.当我尝试控制日志宽度和高度时,我得到了未定义。

How can I get the height and width of div within bootstrap dialog?如何在引导对话框中获取 div 的高度和宽度?

The problem will be that your wrapperRefPopup.current becomes undefined initially and youre fallbacking to 0, reading 0.width/0.height is undefined.问题将是您的 wrapperRefPopup.current 最初变得未定义并且您回退到 0,读取 0.width/0.height 未定义。 Normally refs should be accessable in first useEffect.通常 refs 应该可以在第一次 useEffect 中访问。 This might have something to do with the dialog component.这可能与对话框组件有关。 To insure your ref is set everytime I'd recommend to use useState instead of useRef and have it in the useEffect dep.为了确保每次都设置您的 ref,我建议使用 useState 而不是 useRef 并将其放在 useEffect dep 中。

const [wrapperPopup, setWrapperPopup] = useState(null)

useEffect(() => {
 if(wrapperPopup) {
  const { widht, height } = wrapperPopup.getBoundingClientRect()
 }
}, [wrapperPopup])
...

<div ref={setWrapperPopup}>
...

} }

You can get the width and height via the useRef hook, like so:您可以通过useRef钩子获取widthheight ,如下所示:

// ...
const wrapperRefPopup = useRef();
const [width, setWidth] = useState(0);
const [height, setHeight] = useState(0);

useEffect(() => {
    if (wrapperRefPopup.current) {
        setWidth(wrapperRefPopup.current.clientWidth);
        setHeight(wrapperRefPopup.current.clientHeight);
    }
}, []);

// ...

This is issue with BootstrapDialog这是BootstrapDialog的问题

I kept below code [Actual code for charts] in separate component-我在单独的组件中保留了以下代码[图表的实际代码]-

 <Typography component="div" sx={{
          minWidth: '45rem',
          marginLeft: '4rem',
          marginRight: '4rem',
          paddingTop: '1.5rem',
        }}>
    <div>
        
    <svg ref={svgColorCodeRefPopup} style={{width:"-webkit-fill-available"}}>
</svg>
    </div>
      <div
        ref={wrapperRefPopup}
        style={{ width: "100%", height: "12.5rem", marginBottom: "2rem" }}
      >
        
        <svg ref={svgRefPopup} style={{ width: "100%", height: "110%",marginTop:"0%" }}>
          <g className="x-axis" />
          <g className="y-axis" />
        </svg>
      </div>
      </Typography>

I rendered this separate component having above code from current component having BootstrapDialog我从具有BootstrapDialog的当前组件中渲染了具有上述代码的单独组件

<BootstrapDialog
        style={{height:'auto', zIndex:500}}        
        aria-labelledby="customized-dialog-title"
        open={true}
      >        
        <IconButton
          aria-label="close"
          onClick={closeModal}
          sx={{
            position: 'absolute',
            right: 8,
            top: -5,
            color: (theme) => theme.palette.grey[500],
          }}
        >
          <CloseIcon fontSize="medium" style={{paddingTop:"0.5rem", paddingRight:'0.2rem', color:'#000'}} />
        </IconButton>
    **<SeparateComponent>**
 </BootstrapDialog>

This way it worked.这样它就起作用了。

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

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