我正在尝试显示json(电影对象数组)洞察ListView组件,但收到错误:无法读取属性克隆与未定义的行。我到底做错了什么?我找到了很多教程,它们都使用硬编码数据,当数据来自请求时,看不到它是如何使用的。这是我的代码:
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}
/>
);
}
问题是
dataSource: this.ds.cloneWithRows(data.Search)
应该是
dataSource: this.state.dataSource.cloneWithRows(tutorials)
在 onAfterLoad() 方法中。