【发布时间】:2017-02-26 18:14:04
【问题描述】:
我正在尝试将 React Native <ListView /> 组件与来自 React Native Elements 的 <List /> 和 <ListItem /> 组件一起使用,但 <ListItem /> 组件未显示。不完全确定为什么。我的renderRow 函数不应该为我的数组中的每个对象运行并返回<Listitem /> 吗?我的数据正常。
请让我知道我做错了什么。谢谢!代码如下
import React, { PropTypes, Component } from 'react'
import { View, Text, ListView, StyleSheet } from 'react-native'
import { connect } from 'react-redux'
import { List, ListItem } from 'react-native-elements'
import { getMakeData } from '~/redux/modules/data'
class Make extends Component {
static propTypes = {
dispatch: PropTypes.func.isRequired,
makeData: PropTypes.array.isRequired
}
constructor (props) {
super(props)
this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2 })
this.state = {
dataSource: this.ds.cloneWithRows(this.props.makeData)
}
}
componentDidMount () {
this.props.dispatch(getMakeData())
}
renderRow = (item) => {
return (
<ListItem
key={item.id}
title={item.name}
/>
)
}
render () {
console.log(this.props.makeData)
return (
<List style={{flex: 1}}>
<ListView
renderRow={item => this.renderRow(item)}
dataSource={this.state.dataSource}
/>
</List>
)
}
}
function mapStateToProps ({data}) {
return {
makeData: data.makeData
}
}
export default connect(mapStateToProps)(Make)
const styles = StyleSheet.create({
})
【问题讨论】:
-
你能把
console.log(this.props.makeData)放在render方法中并显示输出吗?? -
@MayankShukla 是的,看起来
componentDidMount没有运行,因为我得到一个空数组作为console.log(this.props.makeData)的输出,在我的组件中的任何内容运行之前,错误就被触发了。我也收到一个错误Warning: Failed prop type: Invalid prop 'dataSource' of type 'Array' supplied to 'ListView', expected instance of 'ListViewDataSource'.
标签: javascript listview reactjs react-native