2017-02-01 54 views
0

我想顯示一個json(電影對象數組)洞察ListView組件,但收到錯誤:無法讀取屬性cloneWithRows的未定義。我究竟做錯了什麼?我發現了很多教程,它們都使用硬編碼數據,並且在數據來自請求時無法看到它是如何使用的。這裏是我的代碼:在ListView中顯示json

constructor() { 
    super(); 
    const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }); 
    this.state = { dataSource: ds.cloneWithRows([]) }; 
} 


componentWillMount() { 
    axios.get('https://www.omdbapi.com/?s=Batman&page=2') 
     .then(response => response.data) 
     .then(this.onAfterLoad) 
     .catch(this.manageError); 
} 

// We will update the state of the application so that images can render 
onAfterLoad = (data) => { 
    this.setState({ dataSource: this.ds.cloneWithRows(data.Search) }); 
}; 

// If there is an error show error  
manageError = (error) => { 
    Alert.alert('Failure fetching data'); 
    console.error(error); 
}; 

// Description: Render all the titles 
// ------------------------------------------------------------------------------------ 

renderRow(rowData) { 
    console.log(rowData); 
    return <Movie key={rowData.imdbID} data={rowData} />; 
} 

// Description: Render everything to the screen 
// ------------------------------------------------------------------------------------ 

render() { 
    return (
     <ListView 
      dataSource={this.state.dataSource} 
      renderRow={this.renderRow} 
     /> 
    ); 
} 

回答

0

的問題是,

dataSource: this.ds.cloneWithRows(data.Search) 

應該

dataSource: this.state.dataSource.cloneWithRows(tutorials) 

內onAfterLoad()方法。