简体   繁体   English

反应:this.props.item 未定义

[英]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. App.js 从该文件中,我尝试将 this.state.searchResults 传递给 SearchResults 组件。

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 SearchResults.js 在这里我尝试将收到的 this.props.searchResults 传递给 TrackList 组件

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 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.但是,如果我在 this.props.tracks 上使用 map 方法,则表示此方法不能用于未定义。 What is wrong?怎么了?

Try to replace with the below code in TrackList.js尝试用 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此外,如果您想通过 props 直接在 React 中的组件之间共享数据,您可以使用React Context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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