繁体   English   中英

从JSON数据填充React-Native ListView

[英]Populating React-Native ListView from JSON Data

在这里,我是React-Native业余爱好者,我写了一个php API从MySQL服务器上检索数据,我需要将其放入ListView中以显示在应用程序上。

显示此代码是:

 constructor(props) { super(props); var ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }); this.state = { dataSource: ds.cloneWithRows(this.props.response) }; } renderRow(rowData) { return ( <View style={styles.row}> <Text>{response}</Text> </View> ) } render() { return { <ListView dataSource={this.state.dataSource} renderRow={this.renderRow.bind(this)}/> ); } } 

API json返回如下:

[{"barcode":"50201600","name":"Cadbury's Creme 
Egg","tescoPrice":"1","sainsburysPrice":"1","asdaPrice":"1"},
{"barcode":"5034660520191","name":"Cadburys Twirl 4 
Pack","tescoPrice":"1","sainsburysPrice":"1","asdaPrice":"1"}]

我得到的错误是: Objects are not valid as a React child (found: object with keys {barcode, name, tescoPrice, sainsburysPrice, asdaPrice}). If you meant to render a collection of children, use an array instead. Check the render method of 'Text' Objects are not valid as a React child (found: object with keys {barcode, name, tescoPrice, sainsburysPrice, asdaPrice}). If you meant to render a collection of children, use an array instead. Check the render method of 'Text'

有任何想法吗?

干杯

根据我看到的错误,您需要更改:

 renderRow(rowData) { return ( <View style={styles.row}> <Text>{response}</Text> </View> ) } 

变成类似:

  renderRow(rowData) { return ( <View style={styles.row}> <Text>{rowData.name}</Text> </View> ) } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM