nicvlas
nicvlas

Reputation: 25

How to get a specific value in an Object in React Native?

I'm currently coding my first app using React Native and using iTunes' search API.

I put a TextInput and a Button which returns all the albums corresponding to the text. Once you click an album, it takes its unique ID and searches the details of this album.

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

Here is the class:


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>
    )
  }
}

If my album ID is '1474669063', the function will return that:

API's response

But here is my problem. Maybe it's super simple but I've tried everything I found but I can't access the data...

Could someone help me please?

Upvotes: 2

Views: 4888

Answers (2)

RIYAJ KHAN
RIYAJ KHAN

Reputation: 15292

The problem is in function getAlbumDetailFromApi.

It does not return the response. It's returning promise.

Considering your code, you should use async/await and return the response only. So, your setState have data part.

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

Upvotes: 0

Tim
Tim

Reputation: 10709

Fetching:

  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));
  }

Render Method:

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>
  );
  }

Demo

https://snack.expo.io/aKlDZLLbF

Upvotes: 1

Related Questions