【问题标题】:React Native - unexpected identifierReact Native - 意外的标识符
【发布时间】:2015-06-01 05:15:43
【问题描述】:

我在以下程序的 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)

我做错了什么?

【问题讨论】:

  • 以防万一人们误解:{....} = React 是一个 ES6 解构赋值,是的,它是有效的。
  • 您知道,@tldr,如果您将个人资料更新为仅是“tldr;”的完整定义,那就太好了根据城市词典。在阅读您的个人资料时,每个人都会意识到他们花了太多时间阅读它。 :D
  • (@T.J.Crowder 是的,我的内部 JSLint 还没有完全更新 :)
  • 我知道玩 Coffeescript 总有一天会得到回报

标签: javascript reactjs ecmascript-6 react-native


【解决方案1】:

您将class 构造视为一系列以逗号分隔的var-like 函数表达式,但这不是class 的工作方式。类中的方法定义更像函数声明,你不要在它们后面加上逗号,因为它们不是一个大表达式的一部分,就像var 之后的变量列表一样。

class movieList extends Component{

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

  fetchMovies() {
    // ...
  }

  // ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2018-11-28
    • 2015-09-14
    • 2017-09-05
    • 2020-07-10
    相关资源
    最近更新 更多