2017-06-06 127 views
0

React-Native业余爱好者在这里,我写了一个php api来从MySQL服务器中检索数据,并且我需要将它放入ListView以显示在应用程序中。从JSON数据填充React-Native 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'

有什么建议吗?

干杯

+0

问题是什么?你有什么尝试?有没有错误信息?我们不在这里为你做你的功课... – c4k

+0

@ c4k我指定了我需要做的问题形式。我需要将来自API的json数据返回到列表视图中以显示产品。 所示的错误消息是“对象是不是一个做出反应的孩子有效(发现:与键{条码,名称,tescoPrice,sainsburysPrice,asdaPrice}对象)! 这不是功课,这是一个个人项目谢谢:) –

+0

什么如果你改变' {response}'到' {response.name}'例如? – c4k

回答

0

从错误中我明白了,你需要改变的:

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

成类似:

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

+0

我仍然得到相同的对象由于某种原因,React子错误无效 –

+0

你可以'console.log(rowData)'< - 什么是行数据 –

+0

当我得到的对象是无效的作为一个React子错误我无法从文件中记录任何内容。 –