出于设计和交互目的,我正在尝试将工作的FlatList替换为基本的ScrollView。但是,即使通过遵循工作示例,我也无法呈现我的列表!
应用程序编译,没有错误,没有警告,但没有列表。
我的代码:
import React from 'react';
import {
View,
ScrollView
} from 'react-native';
import styles from '../../shared-styles/styles.js';
import RecordComponent from './components/record-component.js';
export default class RecordsScreen extends React.Component {
_renderListItems() {
const dataStub = [{
title: 'Hello',
text: 'My first item',
value: '110 Kg',
key: '0'
},
{
title: 'Hello',
text: 'My first item',
value: '110 Kg',
key: '1'
},
{
title: 'Hello',
text: 'My first item',
value: '110 Kg',
key: '2'
},
{
title: 'Hello',
text: 'My first item',
value: '110 Kg',
key: '3'
},
{
title: 'Hello',
text: 'My first item',
value: '110 Kg',
key: '4'
},
{
title: 'Hello',
text: 'My first item',
value: '110 Kg',
key: '5'
},
{
title: 'Hello',
text: 'My first item',
value: '110 Kg',
key: '6'
},
{
title: 'Hello',
text: 'My first item',
value: '110 Kg',
key: '7'
},
{
title: 'Hello',
text: 'My first item',
value: '110 Kg',
key: '8'
},
{
title: 'Hello',
text: 'My first item',
value: '110 Kg',
key: '91'
},
];
return (
dataStub.map(item => { <
RecordComponent record = {
item
}
/>
})
)
};
render() {
return ( <
View style = {
styles.container
} >
<
ScrollView >
// Nothing happens
{
this._renderListItems()
} <
/ScrollView> <
/View>
);
}
}
我的记录组件:
export default class RecordComponent extends React.Component {
render() {
return (
<View key={this.props.record.key} style={[styles.recordContainer, {paddingTop: this.props.style}]}>
<View>
<Text style={styles.recordTitle}>{this.props.record.title}</Text>
<Text style={styles.recordText}>{this.props.record.text}</Text>
</View>
<Text style={styles.recordValue}>{this.props.record.value}</Text>
</View>
);
}
}
我的风格:
container: {
flex: 1,
backgroundColor: '#212121',
marginBottom: -8
},
recordContainer: {
justifyContent: 'space-between',
width: '100%',
padding: 16,
flexDirection: 'row'
},
recordTitle: {
fontWeight: '500',
color: '#ffffff'
},
recordText: {
fontWeight: '400',
color: '#ffffff',
opacity: .5
},
recordValue: {
fontWeight: 'bold',
color: '#EEFF41',
fontSize: 24,
alignSelf: 'center'
},
我按照相同的方法将我的列表呈现为 https://snack.expo.io/BJYG3iFFZ
我尽量不使用自定义组件,结果相同。还尝试仅呈现带有文本项目标题的简单视图,但没有成功...平面列表运行良好,为什么滚动视图没有?
我错过了什么??
好的,
没关系!
我的地图方法中缺少返回,如下所示:
_renderListItems() {
const dataStub = [
{title: 'Hello', text: 'My first item', value: '110 Kg', key: '0'},
{title: 'Hello', text: 'My first item', value: '110 Kg', key: '1'},
{title: 'Hello', text: 'My first item', value: '110 Kg', key: '2'},
{title: 'Hello', text: 'My first item', value: '110 Kg', key: '3'},
{title: 'Hello', text: 'My first item', value: '110 Kg', key: '4'},
{title: 'Hello', text: 'My first item', value: '110 Kg', key: '5'},
{title: 'Hello', text: 'My first item', value: '110 Kg', key: '6'},
{title: 'Hello', text: 'My first item', value: '110 Kg', key: '7'},
{title: 'Hello', text: 'My first item', value: '110 Kg', key: '8'},
{title: 'Hello', text: 'My first item', value: '110 Kg', key: '91'},
];
return (
dataStub.map((item, index) => {
// missed this bad boy !
return (
<RecordComponent key={index} record={item} />
)
})
)
};