简体   繁体   English

.map 在 object 数组上未定义

[英].map undefined on array of object

I am passing array of object as a prop from App.js -> searchResult -> TrackList.js.我正在传递 object 数组作为来自 App.js -> searchResult -> TrackList.js 的道具。 But when I apply.map function on the array of object it shows Cannot read property 'map' of undefined .但是当我在 object 的数组上申请时,它显示无法读取属性“映射”的未定义 I have tried different ways to solve this but none of them worked.我尝试了不同的方法来解决这个问题,但都没有奏效。 In console I am getting the value of prop.在控制台中,我得到了道具的价值。 And my TRackList.js component is rendering four times on a single run.我的 TTrackList.js 组件在一次运行中渲染了四次。 Here is the code这是代码

App.js应用程序.js


 this.state = {
      searchResults: [
        {
          id: 1,
          name: "ritik",
          artist: "melilow"
        },
        {
          id: 2,
          name: "par",
          artist: "ron"
        },
        {
          id: 3,
          name: "make",
          artist: "zay z"
        }
      ]

return ( <SearchResults searchResults={this.state.searchResults} /> )

In Searchresult.js <TrackList tracked={this.props.searchResults} />在 Searchresult.js <TrackList tracked={this.props.searchResults} />

In TrackList.js在 TrackList.js 中

import React from "react";
import Track from "./Track";
export default class TrackList extends React.Component {
  constructor(props) {
    super();
  }
  render() {
 console.log("here",  this.props.tracked);
    return (
      <div>
        <div className="TrackList">
          {this.props.tracked.map(track => {
            return (<Track track={track} key={track.id} />);
          })}
        </div>
      </div>
    );
  }
}

Here is the full code -- https://codesandbox.io/s/jamming-ygs5n?file=/src/components/TrackList.js:0-431这是完整的代码——https://codesandbox.io/s/jamming-ygs5n?file=/src/components/TrackList.js:0-431

You were loading the Component TrackList twice.您正在加载 Component TrackList两次。 One time with no property passed, that's why it was first set in console then looks like it's unset, but it's just a second log.有一次没有通过任何属性,这就是为什么它首先在控制台中设置然后看起来它没有设置,但它只是第二个日志。 I have updated your code.我已经更新了你的代码。 Take a look here https://codesandbox.io/s/jamming-ddc6l?file=/src/components/PlayList.js看看这里https://codesandbox.io/s/jamming-ddc6l?file=/src/components/PlayList.js

You need to check this.props.tracked.map is exists before the loop.您需要在循环之前检查 this.props.tracked.map 是否存在。

Solution Sandbox link: https://codesandbox.io/s/jamming-spf7f?file=/src/components/TrackList.js解决方案沙箱链接: https://codesandbox.io/s/jamming-spf7f?file=/src/components/TrackList.js

import React from "react";
import Track from "./Track";
import PropTypes from 'prop-types';

export default class TrackList extends React.Component {
  constructor(props) {
    super();
  }
  render() {
    console.log("here", typeof this.props.tracked);
    return (
      <div>
        <div className="TrackList">
           {this.props.tracked && this.props.tracked.map(track => {
            return <Track track={track} key={track.id} />;
          })} 
        </div>
      </div>
    );
  }
}

TrackList.propTypes = {
  tracked: PropTypes.arrayOf(PropTypes.shape({
    name: PropTypes.string,
    artist: PropTypes.string,
  }))
};

You need to check this.props.tracked value before implementing the map function.在实现 map function 之前,您需要检查 this.props.tracked 的值。

you can simply check using this.props.tracked && follow your code.您可以使用this.props.tracked &&简单地检查您的代码。

You should add searchResults={this.state.searchResults} in your app.js to Playlist, take it in Playlist with props, and then set it in TrackList from Playlist ( tracked={props.searchResults} ).您应该在app.js中将searchResults={this.state.searchResults}添加到播放列表中,将其放入带有道具的播放列表中,然后从播放列表( tracked={props.searchResults} )中将其设置在 TrackList 中。

Also, Typescript helps me not to do such mistakes.另外,Typescript 帮助我不要犯这样的错误。 Also, add a key prop to your component that you return in the map function.此外,向您在map function 中返回的组件添加一个密钥道具。

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

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