>我试图在我的列表视图中查看项目"披萨",但我收到错误。我查看了其他一些具有相同错误的SO帖子,但我无法找出问题所在。我们应该如何设置我的列表视图?我一直在遵循本指南以供参考。我的代码如下:
class MyNewAppreactold extends Component {
constructor(props) {
super(props);
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
})
};
}
componentDidMount() {
this.setState({
dataSource: this.state.dataSource.cloneWithRows([{ title: 'Pizza' }])
})
}
_renderItem(item) {
return (
<ListItem item={item} />
// <ListItem item={item} onPress={onPress} />
// <ListItem item={item} onPress={() ==""> {}} />
);
}
render() {
return (
<View style={styles.container}>
<StatusBar title="Grocery List" />
<ListView datasource={this.state.dataSource}
renderrow={this._renderItem.bind(this)}
style={styles.listview}/>
<ActionButton title="Add" />
</View>
)
}
}
AppRegistry.registerComponent('MyNewAppreactold', () => MyNewAppreactold);
您是否尝试更改为dataSource
?
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <ListItem item={rowData} />}
/>
或
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow} />
然后
renderRow(rowData) {
return (
<ListItem item={item} />
);
}
请使用正确的道具名称:将 datasourse 更改为 dataSource,将 renderrow 更改为 renderRow。
然后,此代码可以正常工作。
<ListView dataSource={this.state.dataSource}
renderRow={this._renderItem.bind(this)}
style={styles.listview}/>
如果有人偶然发现了这个问题,
- ListView 的 "cloneWithRows" 绑定在 "componentDidMount" 中不起作用。您需要将其推送到"componentWillMount"下。我不知道为什么,但我花了相当多的时间来弄清楚:-(