![](/img/trans.png)
[英]Why does error appear when I try to iterate array of objects in my react.js component?
[英]Why does it return undefined when I try to get the height and width of an image in react.js
我正在嘗試將圖像的高度和寬度值記錄到 react.js 中的控制台,但它們(由於某種原因)未定義。 你知道為什么嗎?
這是代碼:
export default function FilmPage(props) {
const getImgSize = (img) => {
let imageEl = <img onLoad={()=>{
console.log(imageEl.width)
console.log(imageEl.height)
}} src={img} />
return imageEl
}
return (
<div className="proper-film">
<div className="body">
<div>{getImgSize(props.data.image)}</div>
</div>
</div>
)
}
而且我檢索的數據看起來像這樣:
data = {
some: null,
other: null,
stuff: null,
image: "https://my_api.com/images/original/picture.jpg",
and_more: null
}
順便說一句,當我嘗試調試時,我檢索到的數據沒有問題。
你會推薦什么?
您可以修改getImgSize
方法,如下所示
const getImgSize = (img) => {
return <img onLoad={(e) => {
console.log(e.target.offsetHeight)
console.log(e.target.offsetWidth)
}} src={img} />
}
https://codesandbox.io/s/xenodochial-borg-x474m?file=/src/App.js
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.