【问题标题】:How to get an image's width and height after scaling & rendering the react component缩放和渲染反应组件后如何获取图像的宽度和高度
【发布时间】:2021-08-07 08:55:44
【问题描述】:

如何在缩放和渲染 React 组件后获取图像的宽度和高度

  • 获取图像宽度和高度后。我必须更新状态。

【问题讨论】:

  • ref 传递给图像,然后使用该引用,获取useEffect 挂钩内的高度和宽度。

标签: reactjs width


【解决方案1】:
// equivalent to componentDidMount
useEffect(() => {
  const height = document.getElementById("myImg").clientHeight
  const width =document.getElementById("myImg").clientWidth
  // update the state
}, [])

我不知道为什么你必须在渲染后获取宽度和高度,这听起来不是一件好事。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多