这肯定是一个常见的问题,但我不能使用数组源渲染FlatList。
我按照这个例子:https://medium.com/javascript-in-plain-english/how-to-loop-through-arrays-in-react-3eaa8a14445
该示例以:
let shoppingCart = [
{id: 35, item: 'jumper', color: 'red', size: 'medium', price: 20},
{id: 42, item: 'shirt', color: 'blue', size: 'medium', price: 15},
{id: 71, item: 'socks', color: 'black', size: 'all', price: 5},
]
这就是我如何在我的工作中填充数组。
然后,我按照说明进行安排:
this.items = this.shoppingCart.map((item, key) =>
<View key={item.id}/> <Text>{item.name}</Text></View>
);
最后,我将项目设置为FlatList的源:
render(){
return (
<FlatList style = {{flex : 1}}
data={this.items}
keyExtractor={(item, index) => index}
renderItem={({ item })=> {
return item; }
}/>
)
}
我不明白为什么我有一个空白的屏幕......我试图应用不同的风格,但它没有显示任何内容。
编辑
所以,我只是尝试了另一个例子:
render(){
let shoppingCart = [
{id: 35, item: 'jumper', color: 'red', size: 'medium', price: 20},
{id: 42, item: 'shirt', color: 'blue', size: 'medium', price: 15},
{id: 71, item: 'socks', color: 'black', size: 'all', price: 5},
]
const _renderItem = ({item}) => <Text>{item.color}</Text>
return (
<View style = {{background = '#444',flex : 1}}>
<FlatList
data = {shoppingCart}
renderItem = {_renderItem}/>
</View>
)
}
现在我有一个空白屏幕,警告有关index.js未找到,即使我重置缓存...
这是非常令人不安的,我在业务逻辑和复杂计算方面做了大量工作,但我无法呈现一个简单的列表......
在这一点上有人可以帮助我吗?
编辑
我无法解释,现在,我有这样的信息:
Looking for JS files in
D:\Projects\VSCode\projekt\node_modules\react-native\scripts
Loading dependency graph, done.
::ffff:127.0.0.1 - - [26/Mar/2019:01:30:19 +0000] "GET /launch-js- devtools HTTP/1.1" 200 - "-" "okhttp/3.12.1"
Error: Unable to resolve module `./index` from `D:\Projects\VSCode\projekt\node_modules\react-native\scripts/.`: The module `./index` could not be found from `D:\Projects\VSCode\projekt\node_modules\react-native\scripts/.`. Indeed, none of these files exist:
* `D:\Projects\VSCode\projekt\node_modules\react-native\scripts\index(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)`
* `D:\Projects\VSCode\projekt\node_modules\react-native\scripts\index\index(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)`
at ModuleResolver.resolveDependency (D:\Projects\VSCode\projekt\node_modules\metro\src\node-haste\DependencyGraph\ModuleResolution.js:163:15)
at ResolutionRequest.resolveDependency (D:\Projects\VSCode\projekt\node_modules\metro\src\node-haste\DependencyGraph\ResolutionRequest.js:52:18)
at DependencyGraph.resolveDependency (D:\Projects\VSCode\projekt\node_modules\metro\src\node-haste\DependencyGraph.js:283:16)
at D:\Projects\VSCode\projekt\node_modules\metro\src\lib\transformHelpers.js:261:42
at Server.<anonymous> (D:\Projects\VSCode\projekt\node_modules\metro\src\Server.js:1038:41)
at Generator.next (<anonymous>)
at asyncGeneratorStep (D:\Projects\VSCode\projekt\node_modules\metro\src\Server.js:99:24)
at _next (D:\Projects\VSCode\projekt\node_modules\metro\src\Server.js:119:9)
::1 - - [26/Mar/2019:01:30:20 +0000] "GET /index.delta?platform=android&dev=true&minify=false&revisionId=291a9787399f7066 HTTP/1.1" 500 - "http://localhost:8081/debugger-ui/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
::ffff:127.0.0.1 - - [26/Mar/2019:01:30:29 +0000] "GET /onchange HTTP/1.1" - - "-" "okhttp/3.12.1"
::ffff:127.0.0.1 - - [26/Mar/2019:01:30:44 +0000] "GET /onchange HTTP/1.1" - - "-" "okhttp/3.12.1"
::ffff:127.0.0.1 - - [26/Mar/2019:01:30:59 +0000] "GET /onchange HTTP/1.1" - - "-" "okhttp/3.12.1"
为什么一个简单的FlatList会导致这么多问题?再一次,我尽我所能:重置缓存,删除noe_modules,npm安装,npm链接......但......没有任何作用......
编辑
对不起,很长的问题,但我的天意,React Native与我的前妻相似:按月计算10分钟,其余时间是痛苦和问题......我现在处于循环中:我解决了index.js问题,然后出现antoher问题:无法从'index.js找到模块'@ babel / runtime / helpers / builtin / interopRequireDefault'。我解决了......而且......是的..再次出现.index.js问题...... pfff ......这是什么?
最后的编辑...答案
最后,我意识到网上给出的一些例子不起作用;写入语法错误,无法运行它们。我用很多痛苦解决了错误循环。检查我的整个代码,在某处我执行了导入:从'./classeX'导入{classX} ...我替换为:从./classeX导入classX并且我的世界改变了...它工作了......所以经过大量测试,下面的代码对我有用:
import React, { Component } from 'react';
import {StyleSheet,View,FlatList, Text, Image } from 'react-native';
export default class Test extends Component {
constructor(props) {
super(props);
};
sampleData = [
{
name: { title: 'mr', first: 'karl', last: 'johnson' },
email: '[email protected]',
picture: {
thumbnail: 'https://randomuser.me/api/portraits/thumb/men/62.jpg',
},
},
{
name: { title: 'mrs', first: 'asuncion', last: 'gomez' },
email: '[email protected]',
picture: {
thumbnail: 'https://randomuser.me/api/portraits/thumb/women/52.jpg',
},
nat: 'ES',
},
{
name: { title: 'miss', first: 'gilcenira', last: 'ribeiro' },
email: '[email protected]',
picture: {
thumbnail: 'https://randomuser.me/api/portraits/thumb/women/21.jpg',
},
},]
renderItem = ({item, index}) =>
<View style={styles.row}>
<Image style={styles.picture} source={{ uri: item.picture.thumbnail }} />
<View>
<Text style={styles.primaryText}>
{item.name.last + ' ' + item.first}
</Text>
<Text style={styles.secondaryText}>{item.email}</Text>
</View>
</View>
render() {
return(
<View style ={styles.container}>
<Text style = {styles.description}>ok</Text>
<FlatList style={{backgroundColor :'#444',
color : '#fff'}}
data={this.sampleData}
renderItem={this._renderItem}
keyExtractor={(item =>item.email)}/>
</View>)
}
}
const styles = StyleSheet.create({
container : {
flex : 1,
alignItems : 'center',
},
row: {
flexDirection: 'row',
alignItems: 'center',
padding: 12
},
picture: {
width: 50,
height: 50,
borderRadius: 25,
marginRight: 18
},
primaryText: {
fontWeight: 'bold',
fontSize: 14,
color: 'violet',
marginBottom: 4,
},
secondaryText: {
color: 'darkgrey'
},
container : {
flex : 1,
alignItems : 'center',
backgroundColor :'#666',
flexDirection : 'column'
},
description: {
fontSize: 20,
color : '#fff',
textAlign: 'center',
marginTop: 8,
marginBottom : 8,
top : 4
},
});
如果它可以帮助某人......
您可以尝试在renderItem中返回值。例:
renderItem = ({item, index}) => (
<View style={styles.row}>
<Image style={styles.picture} source={{ uri: item.picture.thumbnail }} />
<View>
<Text style={styles.primaryText}>
{item.name.last + ' ' + item.first}
</Text>
<Text style={styles.secondaryText}>{item.email}</Text>
</View>
</View>);
在Flalist中应该是:
<FlatList
contentContainerStyle={{backgroundColor :'#444'}}
data={this.sampleData}
renderItem={this.renderItem}
keyExtractor={(item =>item.email)}
/>