简体   繁体   English

React Native - 意外标识符

[英]React Native - unexpected identifier

I'm getting an unexpected identifier error on the line fetchMovies() { in the following program: 我在fetchMovies() {行中fetchMovies() {意外的标识符错误fetchMovies() {在以下程序中:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */
'use strict';

var React = require('react-native'),

{
  StyleSheet,
  Component,
  AppRegistry,
  ListView,
  View,
  Text,
  Image
} = React,

baseUrl = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json',
apiKey = '7waqfqbprs7pajbz28mqf6vz',
pageLimit = 25,
queryString = '?apikey=' + apiKey + '&page_limit=' + pageLimit,
url = baseUrl + queryString,

styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'white',
  },
  rightContainer: {
    flex: 1,
  },
  title: {
    marginBottom: 8,
    textAlign: 'center'
  },
  year: {
    fontSize: 10,
    textAlign: 'center'
  },
  thumbnail: {
    width: 53,
    height: 81
  },
  listView: {
    paddingTop: 20,
    backgroundColor: 'black'
  }
})

class movieList extends Component{

  getInitialState() {
    return {
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2
      }),
      loaded: false
    }
  },

  fetchMovies() {
    return fetch(url)
    .then((response) => response.json())
    .then((data) => data.movies)
  },

  componentDidMount() {
    this.fetchMovies()
    .then((movies) => {
      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(movies),
        loaded: true
      })
    })
    .done()
  },

  getLoadingView() {
    return (
      <View style={styles.container}>
        <Text>
          Loading Movies...
        </Text>
      </View>
    )
  },

  renderMovie(movie) {
    if (!this.state.loaded) {
      return this.getLoadingView()
    }

    return (
      <View style={styles.container}>
        <Image
          source={{uri: movie.posters.thumbnail}}
          style={styles.thumbnail}
        />
        <View style={styles.rightContainer}>
          <Text style={styles.title}>{movie.title}</Text>
          <Text style={styles.year}>{movie.year}</Text>
        </View>
      </View>
    )
  },

  render() {
    return (
      <ListView
        dataSource={this.state.dataSource}
        renderRow={this.renderMovie}
        style={styles.listView}
      />
    )
  }

}

AppRegistry.registerComponent('movieList', () => movieList)

What am I doing wrong? 我究竟做错了什么?

You're treating the class construct as though it were a series of comma-separated var -like function expressions, but that's not how class works. 您正在将class构造视为一系列逗号分隔的类似var的函数表达式,但这不是class工作方式。 The method definitions in a class are more like function declarations , and you don't put commas after them, because they're not part of one big expression as the variable list after var is. 类中的方法定义更像是函数声明 ,并且不会在它们之后放置逗号,因为它们不是var之后变量列表的一个大表达式的一部分。

class movieList extends Component{

  getInitialState() {
    return {
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2
      }),
      loaded: false
    }
  } // <=== No comma here

  fetchMovies() {
    // ...
  }

  // ...
}

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

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