I have this code. I imported some components from Semantic UI.
import React from 'react'
import { Card, Image, Grid } from 'semantic-ui-react'
I am trying to call function when error (404) appears when loading image.
export default class HotelCards extends React.Component {
// constructor
constructor(props){
super(props)
this.handleError = this.handleError.bind(this);
}
// state
state = {}
This is the function I'd like to call: (not that if I log this inside render function I get instance of current class)
handleError() {
console.log(this);
}
render() {
if (!this.props.hotels) return null;
return (
{
this.props.hotels.map(function(e, i) {
return (
From this element:
)
})
}
);
}//render
}//class
However I get error that this is undefined.
TypeError: this is undefined
Stack trace:
render/
...
In vanilla JavaScript my approach for this would be
How do I bind this function to component properly?
本文探讨了在React应用中如何正确地处理图片加载失败的情况,并实现特定的错误回调函数。作者通过使用Semantic UI React组件库中的元素演示了这一过程,并讨论了在类组件中绑定方法以确保`this`关键字正确引用的重要性。
1万+

被折叠的 条评论
为什么被折叠?



