3

I've weird issue when using map() function when using it with ReactJS. I have array of 3 objects hardcoded into my App state and when i try to iterate throught each object I can see the data in the console. It looks like that TrackList.js is being rendered again and then spits an undefined error.
Here's my main files:

App.js:

import React, { Component } from 'react';
import './App.css';
import SearchBar from '../SearchBar/SearchBar';
import SearchResults from '../SearchResults/SearchResults';
import Playlist from '../Playlist/Playlist';
// import songs from './songs.js';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      searchResults: [
        {
          "id": 1,
          "name": "Roses",
          "artist": "The Chainsmokers",
          "album": "Unknown",
          "isRemoval": false
        },
        {
          "id": 2,
          "name": "Fight Song",
          "artist": "Rachel Platten",
          "album": "None",
          "isRemoval": false
        },
        {
          "id": 3,
          "name": "Thunder",
          "artist": "Imagine Dragons",
          "album": "Evolve",
          "isRemoval": false
        }
      ]
    };
  }

  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;

Searchresults.js:

import React from 'react';
import './SearchResults.css';
import TrackList from '../TrackList/TrackList';

class SearchResults extends React.Component {
  render() {
    return (
      <div className="SearchResults">
        <h2>Results</h2>
        <TrackList tracks={this.props.searchResults} />
      </div>
    );
  }
}

export default SearchResults;

TrackList.js:

import React from 'react';
import './TrackList.css';
import Track from '../Track/Track';


class TrackList extends React.Component {
  render() {
    return (
      <div className="TrackList">
          {console.log("Tracks:")}
          {console.log(this.props.tracks)}
          {
            this.props.tracks.map(track => {
              return <Track track={track} key={track.id} />;
            })
          }
      </div>
    );
  }
}

export default TrackList;

Track.js:

import React from 'react';
import './Track.css';


class Track extends React.Component {
  constructor(props) {
    super(props);
    this.renderAction = this.renderAction.bind(this);
  }

  renderAction(isRemoval) {
    if (isRemoval) {
      return "-";
    }
    return "+";
  }

  render() {
    return (
      <div className="Track">
        <div className="Track-information">
        {console.log("Each track:")}
        {console.log(this.props.track)}
          <h3>{this.props.track.name}</h3>
          <p>{this.props.track.artist} | {this.props.track.album}</p>
        </div>
        <a className="Track-action">{this.renderAction(this.props.track.isRemoval)}</a>
      </div>
    );
  }
}

export default Track;

Console.log():

Tracks:
Array [ {…}, {…}, {…} ]
Each track:
Object { id: 1, name: "Roses", artist: "The Chainsmokers", album: "Unknown" }
Each track:
Object { id: 2, name: "Fight Song", artist: "Rachel Platten", album: "None" }
Each track:
Object { id: 3, name: "Thunder", artist: "Imagine Dragons", album: "Evolve" }
Tracks:
undefined
TypeError: this.props.tracks is undefined
4
  • Maybe check if this.props.tracks is undefined and just return null when it is. I'm not sure why you get an undefined here, but I know that React renders components a few times. Commented Dec 23, 2017 at 14:31
  • i found an issue. it was called by another component which i didn't listed, which i thought has nothing to do with it, because I didn't touched it when i was working with App component state. thanks for quick replies Commented Dec 23, 2017 at 14:35
  • Awesome! Those problems where you think you checked everything except one place suck the most :D We’ve all been more than once. Commented Dec 23, 2017 at 15:08
  • yeah, i spent 3 hours before posting here. and then after 10 more minutes I found the problem myself :D Commented Dec 23, 2017 at 15:52

1 Answer 1

1

Try replacing the this.props.tracks.map(track => { line with the following line,

this.props.tracks && this.props.tracks.map(track => {
    // code continues
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.