[英]Populating React-Native ListView from JSON Data
React-Native amateur here, I've written a php api to retrieve data from a MySQL server, and I need to put this into a ListView to display on an app. 在这里,我是React-Native业余爱好者,我写了一个php API从MySQL服务器上检索数据,我需要将其放入ListView中以显示在应用程序上。
The code for showing this is: 显示此代码是:
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)}/> ); } }
And the API json return is as follows: 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"}]
The error I get is: 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'
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'
Any ideas? 有任何想法吗?
Cheers 干杯
From the error i see, you need to change from: 根据我看到的错误,您需要更改:
renderRow(rowData) { return ( <View style={styles.row}> <Text>{response}</Text> </View> ) }
into something like: 变成类似:
renderRow(rowData) { return ( <View style={styles.row}> <Text>{rowData.name}</Text> </View> ) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.