I'm following a React JS tutorial and I'm attempting to pass props from one component to another in order to render a list of tracks. I've looked at few threads with similar issues but none of the suggestions have resolved my issue and I'm at a loss. I suspect the issue has something to do with how my initial state is set.
I've attempted the following.
Cannot read property 'map' of undefined REACT
- Changing 'this.props' to 'this.state' and vise versa on receiving components as recommended above but this does not resolve the issue and just returns 'cannot read property tracks of null'
This is my App.js where initial state is defined and passed to the search results component
constructor(props) {
super(props);
this.state = {
searchResults: [
{
name:'test',
artist:'test',
album: 'test',
id:'2'
},
{
name:'test',
artist:'test',
album: 'test',
id:'2'
}
]
}
}
render() {
return (
<div>
<h1>Ja<span className="highlight">mmm</span>ing</h1>
<div className="App">
<SearchBar/>
<div className="App-playlist">
<SearchResults searchResults={this.state.searchResults}/>
<Playlist/>
</div>
</div>
</div>
);
}
}
export default App;
This is where the SearchResults component passes props to TrackList
class SearchResults extends React.Component {
render() {
return(
<div className="SearchResults">
<h2>Results</h2>
<TrackList tracks = {this.props.searchResults}/>
</div>
)
}
}
export default SearchResults;
This is where the error seems to be occurring.
class TrackList extends React.Component {
render() {
return (
<div className="TrackList">
{this.state.tracks.map(track => <Track key={track.id}
track={track} />)}
</div>
);
}
}
export default TrackList;
Lastly the track class that should render a track
class Track extends React.Component {
render(){
return(
<div className="Track">
<div className="Track-information">
<h3>{this.props.track.name}</h3>
<p> {this.props.track.artist} | {this.props.track.album}</p>
</div>
<button className="Track-action">- + or - will go here --></button>
</div>
)
}
}
export default Track;
the output should be a rendered list of tracks, instead I get 'TypeError: Cannot read property 'map' of undefined.
TypeError: Cannot read property 'map' of undefined TrackList.render src/components/TrackList/TrackList.js:11 8 | class TrackList extends React.Component { 9 | render() { 10 | return ( 11 | 12 | {this.props.tracks.map(track => )} 14 |
this.props.tracksinTrackList component instead ofthis.state.tracks`