1

I'm trying to create a simple app the retrieves data from a facebook demo api and displays them with react native.

This is my code (for index.android.js) :

import React, { Component } from 'react';
import { AppRegistry, Text , View } from 'react-native';

class AwesomeProject extends Component{


  constructor(props){
    super(props);
    this.state = {
      movies: []
    }

  };

  componentWillMount(){
      this.getMoviesFromApi().then((res) => {
          movies: res.movies;
      });
  }



   async function getMoviesFromApi() {
    try {
      let response = await fetch('https://facebook.github.io/react-native/movies.json');
      let responseJson = await response.json();
      return responseJson.movies;
    } catch(error) {
      console.error(error);
    }
  }



  render() {

      return(
          <Text>
            {this.state.movies}
          </Text>
        );

  }

}
AppRegistry.registerComponent('AwesomeProject',() => AwesomeProject);

But it keeps giving me this error:

Unexcepted token, excpected ( (28:18) index.android.js:23:18
0

1 Answer 1

1

There are a few mistakes in your code.
1. Use setState to update movies property.

  this.getMoviesFromApi().then((res) => {
    this.setState({
      movies: res
    });
  });

2. async function getMoviesFromApi() should be just async getMoviesFromApi()
3. In render function, wrap Text inside View and loop through movies array. Example -

  return(
    <View>
      {this.state.movies.map(m => (
        <Text key={m.title}> {m.title} </Text>))}
    </View>
  );
Sign up to request clarification or add additional context in comments.

2 Comments

thank you sir. Can you please tell me where to find some good tutorials to get started with react native? I'm really interested in the technology, but I'm a total beginner with react @vinayr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.