这是来自facebook电影示例应用的一段代码。如何在getData函数中获取电影标题。我也使用了TouchableHighlight onPress,但可以获得电影。我可以从每个列表项中获取项目ID。
import React, {Component,} from 'react'; import { AppRegistry, Image, ListView, StyleSheet, Text, View, } from 'react-native'; var REQUEST_URL ='https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json'; export default class SampleAppMovies extends Component { constructor(props) { super(props); this.state = { dataSource: new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2, }), loaded: false, }; } componentDidMount() { this.fetchData(); } fetchData() { fetch(REQUEST_URL) .then((response) => response.json()) .then((responseData) => { this.setState({ dataSource: this.state.dataSource.cloneWithRows(responseData.movies), loaded: true, }); }) .done(); } // I need id movie name here _getData = (name) =>{ console.log(name); } render() { if (!this.state.loaded) { return this.renderLoadingView(); } return ( <ListView dataSource={this.state.dataSource} renderRow={this.renderMovie} /> ); } renderLoadingView() { return ( <View style={styles.container}> <Text> Loading movies... </Text> </View> ); } renderMovie(movie) { return ( <View style={styles.container}> <Image source={{uri: movie.posters.thumbnail}} style={styles.thumbnail} /> <View style={styles.rightContainer}> <Text style={styles.title} onPress={this._getData(movie)}>{movie.title}</Text> <Text style={styles.year}>{movie.year}</Text> </View> </View> ); } }