I have the following code, which is returning the error:
"cannot read property 'albumCover' of undefined
Based on the code at line 24.
import React, {Component} from 'react';
import albumData from './../data/albums';
class Album extends Component {
constructor(props) {
super(props);
const album = albumData.find( album => {
return album.slug === this.props.match.params.slug
});
this.state = {
album: album
};
}
render() {
return (
<section className ="album">
<section id="album-info">
<img id="album-cover-art" src={this.state.album.albumCover} /> <- error
<div className="album-details">
<h1 id="album-title"></h1>
<h2 className="artist"></h2>
<div id="release-info"></div>
</div>
</section>
</section>
);
}
}
export default Album
My understanding is that this is due to a problem with the context of 'this' not being set correctly, but as far as I can see, this code should execute correctly. Can anyone tell my why I'm getting the error, and how to set the right context for 'this'?