简体   繁体   English

React-Native:未处理的 promise 拒绝:错误:对象作为 React 子对象无效

[英]React-Native: Unhandled promise rejection: Error: Objects are not valid as a React child

I have an array of objects that look like the object below and I am trying to render a list.我有一组看起来像下面的 object 的对象,我正在尝试呈现一个列表。 I have other screens that render lists of the same shape in the same manner.我有其他屏幕以相同的方式呈现相同形状的列表。 I cannot figure out why I am getting an error with this array.我无法弄清楚为什么我会收到这个数组的错误。 The promise is handled correctly and the data is set to the state. promise 处理正确,数据设置为 state。 The data that I am trying to render is an array.我要渲染的数据是一个数组。

The error错误

[Unhandled promise rejection: Error: Objects are not valid as a React child (found: object with keys {external_urls, href, id, name, type, uri}). If you meant to render a collection of children, use an array instead.]

The array: this.state.playlist.items数组:this.state.playlist.items

Array [
  Object {
    "added_at": "2020-12-06T06:43:39Z",
    "added_by": Object {
      "external_urls": Object {
        "spotify": "https://open.spotify.com/user/",
      },
      "href": "https://api.spotify.com/v1/users/",
      "id": "",
      "type": "user",
      "uri": "spotify:user:"",
    },
    "is_local": false,
    "primary_color": null,
    "track": Object {
      "album": Object {
        "album_type": "single",
        "artists": Array [
          Object {
            "external_urls": Object {
              "spotify": "https://open.spotify.com/artist/4iMO20EPodreIaEl8qW66y",
            },
            "href": "https://api.spotify.com/v1/artists/4iMO20EPodreIaEl8qW66y",
            "id": "4iMO20EPodreIaEl8qW66y",
            "name": "Still Woozy",
            "type": "artist",
            "uri": "spotify:artist:4iMO20EPodreIaEl8qW66y",
          },
        ],
        "external_urls": Object {
          "spotify": "https://open.spotify.com/album/5mlgzPatuoMGqqHsPIofKr",
        },
        "href": "https://api.spotify.com/v1/albums/5mlgzPatuoMGqqHsPIofKr",
        "id": "5mlgzPatuoMGqqHsPIofKr",
        "images": Array [
          Object {
            "height": 640,
            "url": "https://i.scdn.co/image/ab67616d0000b2738d23ae740afca14480db70c8",
            "width": 640,
          },
          Object {
            "height": 300,
            "url": "https://i.scdn.co/image/ab67616d00001e028d23ae740afca14480db70c8",
            "width": 300,
          },
          Object {
            "height": 64,
            "url": "https://i.scdn.co/image/ab67616d000048518d23ae740afca14480db70c8",
            "width": 64,
          },
        ],
        "name": "Wolfcat",
        "release_date": "2017-08-31",
        "release_date_precision": "day",
        "total_tracks": 1,
        "type": "album",
        "uri": "spotify:album:5mlgzPatuoMGqqHsPIofKr",
      },
      "artists": Array [
        Object {
          "external_urls": Object {
            "spotify": "https://open.spotify.com/artist/4iMO20EPodreIaEl8qW66y",
          },
          "href": "https://api.spotify.com/v1/artists/4iMO20EPodreIaEl8qW66y",
          "id": "4iMO20EPodreIaEl8qW66y",
          "name": "Still Woozy",
          "type": "artist",
          "uri": "spotify:artist:4iMO20EPodreIaEl8qW66y",
        },
      ],
      "disc_number": 1,
      "duration_ms": 174221,
      "episode": false,
      "explicit": true,
      "external_ids": Object {
        "isrc": "QZ9JZ1701572",
      },
      "external_urls": Object {
        "spotify": "https://open.spotify.com/track/1Hu2OypX8tMPwBcCUaAeO4",
      },
      "href": "https://api.spotify.com/v1/tracks/1Hu2OypX8tMPwBcCUaAeO4",
      "id": "1Hu2OypX8tMPwBcCUaAeO4",
      "is_local": false,
      "name": "Wolfcat",
      "popularity": 63,
      "preview_url": "https://p.scdn.co/mp3-preview/b6ac80e632d0c15e097d43083a738bf69ac8bc12?cid=5673f7c36ce34a669e8805ca91f3b103",
      "track": true,
      "track_number": 1,
      "type": "track",
      "uri": "spotify:track:1Hu2OypX8tMPwBcCUaAeO4",
    },
    "video_thumbnail": Object {
      "url": null,
    },
  },

Rendering the list渲染列表

<View style={{marginBottom: 310}}>
  <FlatList
    data = {this.state.playlist.items}
    renderItem = {({ item }) => (
    <ListItem bottomDivider >
      <Avatar size={80} source={{uri: item.track.album.images[0].url}} />
      <ListItem.Content>
        <ListItem.Title>{item.track.name}</ListItem.Title>
        <ListItem.Subtitle>{item.track.album.artists[0]}</ListItem.Subtitle>
      </ListItem.Content>
      <ListItem.Chevron />
    </ListItem>
  )}
  keyExtractor={item => item.track.id}
/>
</View>

The error message is actually quite explanatory.错误消息实际上是非常解释性的。 If you change your code in following way, for instance:如果您以下列方式更改代码,例如:

<ListItem.Subtitle>{item.track.album.artists[0].name}</ListItem.Subtitle>

it should work.它应该工作。 React does not allow to use an Object as a child ( item.track.album.artists[0] is Object). React 不允许使用Object作为孩子( item.track.album.artists[0]是对象)。 Only primitive types are allowed (typically String or Number).只允许使用原始类型(通常是字符串或数字)。 item.track.album.artists[0].name is String . item.track.album.artists[0].nameString

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

相关问题 反应本地可能未处理的诺言拒绝 - Possible unhandled promise rejection in react-native React Native Unhandled promise 拒绝 | React-Native, 异步, - React Native Unhandled promise rejection | React-Native, Async, 反应原生的未处理承诺拒绝错误? - Unhandled promise rejection error in react native? (本机)可能未处理的承诺拒绝FBSDK - (React-Native) Possible Unhandled Promise Rejection FBSDK 可能未处理的 Promise 拒绝 - React-Native with Firebase - Possible Unhandled Promise Rejection - React-Native with Firebase React Native中未处理的承诺拒绝 - Unhandled promise rejection in react native React-Native 对象作为 React 子对象无效 - React-Native Objects are not valid as a React child 未处理的 promise 拒绝:错误:数据库表错误:在本机反应中未定义 - Unhandled promise rejection: Error: Database Table error: undefined in react native React Native - [未处理的 promise 拒绝:错误:注册错误] - React Native - [Unhandled promise rejection: Error: Signup Error] react-native 错误:[未处理的 promise 拒绝:错误:获取 Expo 令牌时遇到错误:TypeError:网络请求失败。] - react-native error:[Unhandled promise rejection: Error: Error encountered while fetching Expo token: TypeError: Network request failed.]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM