我尝试从一个json渲染几行,效果很好,但是当我在查询中为单行着色时,出现以下错误:
TypeError: this is undefined
render/<
C:/Users/.../Code/GeoInfoErgebnisse.js:49
Error:
46 | return (
47 | <tr
48 | key={key}
> 49 | style={
50 | this.isSelected(key)
51 | ? { background: 'red' }
52 | : { background: 'blue' }
代码:
class GeoInfoErgebnisse extends React.Component {
constructor(props) {
super(props);
this.isSelected = this.isSelected.bind(this);
this.state = {
selected: -1,
};
}
isSelected(key) {
return key === this.state.selected;
}
render() {
return (
...
<tbody>
{this.props.items.map(function(item, key) {
return (
<tr key={key}
style={this.isSelected(key) ? { background:'red' } : { background: 'blue' }}>
...
</tr>
);
})}
</tbody>
...
);
}
}