如何在React Native中的对象中获取特定值?

尼古拉斯

我目前正在使用React Native和iTunes的搜索API编写我的第一个应用程序。

我放置了一个TextInput和一个Button,该按钮返回与文本相对应的所有相册。单击相册后,它将获得其唯一的ID,并搜索该相册的详细信息。

export function getAlbumDetailFromApi(id)
{
  return fetch('https://itunes.apple.com/lookup?id='+id)
  .then((response) => response.json())
  .catch((error) => console.log(error));
}

这是课程:


class AlbumDetail extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      album: undefined,
      isLoading: true
    }
  }

  componentDidMount() {
    console.log(this.props.navigation.state.params.idAlbum)
    getAlbumDetailFromApi(this.props.navigation.state.params.idAlbum).then(data => {
      this.setState({
        album: data,
        isLoading: false
      })
    })
  }

  _displayLoading() {
    if (this.state.isLoading) {
      return (
        <View style={styles.loading_container}>
          <ActivityIndicator size='large' />
        </View>
      )
    }
  }

  _displayFilm() {
    const { album } = this.state

    console.log(album.results.artistId)
    console.log()
    if (album != undefined) {
      return (
        <ScrollView style={styles.scrollview_container}>
          <Text style={styles.title_text}>name of the album should go here</Text>     
        </ScrollView>
      )
    }
  }

  render() {
    return (
      <View style={styles.main_container}>
        {this._displayLoading()}
        {this._displayFilm()}
      </View>
    )
  }
}

如果我的相册ID为“ 1474669063”,该函数将返回以下内容:

API的回应

但是,这是我的问题。也许这很简单,但是我已经尝试了所有发现的内容,但是无法访问数据...

有人可以帮我吗?

提姆

正在抓取:

  getAlbumDetailFromApi(id) {
    return fetch('https://itunes.apple.com/lookup?id='+id)
      .then(response => response.json())
      .then(data => {
        // set the state here, we access the first object of the results array with data.results[0}
        this.setState({album: data.results[0]})
        })
    .catch((error) => console.log(error));
  }

渲染方法:

componentDidMount() {
    this.getAlbumDetailFromApi(1474669063);
  }
  renderAlbum() {
    // if we have an album display it 
    if (this.state.album) {
      return (
        <View>
        <Text> Arist: {this.state.album.artistName}</Text>
        <Text> Release Date: {this.state.album.releaseDate}</Text>
        </View>
      );
    }
    
  }
  render() {
    console.log('state', this.state);
    return (
    <View style={styles.container}>
        {this.renderAlbum()}
    </View>
  );
  }

演示版

https://snack.expo.io/aKlDZLLbF

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从数组中获取特定值的对象

来自分类Dev

如何在StyleSheet.create中获取对象?(React Native)

来自分类Dev

如何在react hooks数组中获取其对象的值?

来自分类Dev

如何在React Native中渲染对象?

来自分类Dev

如何在React Native中传递对象?

来自分类Dev

如何在Typescript中获取对象的值

来自分类Dev

如何在 PHP 中获取对象的值

来自分类Dev

如何从嵌套的json数组中获取特定的对象值

来自分类Dev

如何在React Native中获取数组对象内的嵌套数组对象

来自分类Dev

如何在React Native中的Flatlist中获取行索引

来自分类Dev

如何在对象 react-native 中获取数组大小

来自分类Dev

如何在 React 中的数组 onClick 中显示特定对象?

来自分类Dev

如何在React中更新嵌套(对象值的对象)状态

来自分类Dev

如何在React Native中获取Range Slider的当前值?

来自分类Dev

如何在react-native中过滤JavaScript对象

来自分类Dev

我如何在React Native中通过此对象映射?

来自分类Dev

如何在React Native中从单独的组件创建对象数组?

来自分类Dev

如何在 react-native 中访问导航对象

来自分类Dev

如何在 react-native 中呈现 JSON 对象

来自分类Dev

如何在firebase中获取对象的先前值(值)?

来自分类常见问题

如何在React Native中更改特定列的样式?

来自分类Dev

如何在React Native Android中获取响应标头?

来自分类Dev

如何在React Native中显示获取的数据

来自分类Dev

如何在React Native中获取Text组件的行数?

来自分类Dev

我如何在React Native中获取路线参数

来自分类Dev

如何在React Native中获取电话状态

来自分类Dev

如何在 React Native 的 RadioGroup 中获取所选项目?

来自分类Dev

如何在React渲染函数中从JSON对象获取值?

来自分类Dev

如何在React渲染函数中从JSON对象获取值?

Related 相关文章

  1. 1

    如何从数组中获取特定值的对象

  2. 2

    如何在StyleSheet.create中获取对象?(React Native)

  3. 3

    如何在react hooks数组中获取其对象的值?

  4. 4

    如何在React Native中渲染对象?

  5. 5

    如何在React Native中传递对象?

  6. 6

    如何在Typescript中获取对象的值

  7. 7

    如何在 PHP 中获取对象的值

  8. 8

    如何从嵌套的json数组中获取特定的对象值

  9. 9

    如何在React Native中获取数组对象内的嵌套数组对象

  10. 10

    如何在React Native中的Flatlist中获取行索引

  11. 11

    如何在对象 react-native 中获取数组大小

  12. 12

    如何在 React 中的数组 onClick 中显示特定对象?

  13. 13

    如何在React中更新嵌套(对象值的对象)状态

  14. 14

    如何在React Native中获取Range Slider的当前值?

  15. 15

    如何在react-native中过滤JavaScript对象

  16. 16

    我如何在React Native中通过此对象映射?

  17. 17

    如何在React Native中从单独的组件创建对象数组?

  18. 18

    如何在 react-native 中访问导航对象

  19. 19

    如何在 react-native 中呈现 JSON 对象

  20. 20

    如何在firebase中获取对象的先前值(值)?

  21. 21

    如何在React Native中更改特定列的样式?

  22. 22

    如何在React Native Android中获取响应标头?

  23. 23

    如何在React Native中显示获取的数据

  24. 24

    如何在React Native中获取Text组件的行数?

  25. 25

    我如何在React Native中获取路线参数

  26. 26

    如何在React Native中获取电话状态

  27. 27

    如何在 React Native 的 RadioGroup 中获取所选项目?

  28. 28

    如何在React渲染函数中从JSON对象获取值?

  29. 29

    如何在React渲染函数中从JSON对象获取值?

热门标签

归档