简体   繁体   中英

React: this.props.item is undefined

I try to pass an array from one component to another, and from that one to another one, but my item is undefined. App.js From that file I try to pass this.state.searchResults to SearchResults component.

import React from 'react';
import './App.css';
import SearchBar from '../SearchBar/SearchBar';
import SearchResults from '../SearchResults/SearchResults';
import Playlist from '../Playlist/Playlist';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      searchResults: [
        {name: 'name1', artist: 'artist1', album: 'album1', id: 1},
        {name: 'name2', artist: 'artist2', album: 'album2', id: 2},
        {name: 'name3', artist: 'artist3', album: 'album3', id: 3}
      ]
    };
  }

  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 Here I try to pass received this.props.searchResults to TrackList component

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

class SearchResults extends React.Component {
    render() {
        console.log(this.props.searchResults + ' SearchResults');
        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 {
    constructor(props){
        super(props);
        console.log(props);
    }

    render() {
        return(
            <div className="TrackList">
                {this.props.track.map(track => <Track track={track} key={track.id} />)}
            </div>
        );
    }
}

export default Tracklist;

However, if I use map method on this.props.tracks, it says that this method cannot be used on undefined. What is wrong?

Try to replace with the below code in TrackList.js

<div className="TrackList">
   {this.props.track.map(track => <Track track={track} key={track.id} />)}
</div>

Hope this helps

The problem is here

{this.props.track.map(track => <Track track={track} key={track.id} />)}

it should be tracks not track

{this.props.tracks.map(track => <Track track={track} key={track.id} />)}

Also if you want to share data between components in React direct with follow top-down way via props you can use React Context

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM