React Native:平面列表和数组

时间:2019-03-25 23:50:17

标签: arrays react-native-flatlist

这肯定是一个常见问题,但是我无法使用数组源呈现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; }
    }/>   
  )
}

我不明白为什么会有黑屏...我尝试应用不同的样式,但什么也没显示。

EDIT

所以,我只是尝试了另一个示例:

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的警告,即使我重置了缓存...

这很令人不安,我在业务逻辑和复杂的计算上做了大量工作,但是我无法绘制简单的列表...

有人可以帮我吗?

EDIT

我无法解释,现在,我收到此消息:

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链接...但是...什么都不起作用...

EDIT

很抱歉,我想知道的是,我的天哪,React Native与我的前妻相似:每月有10分钟的娱乐时间,其余时间是痛苦和问题...我现在处于以下状态:我解决索引.js问题,然后出现其他问题:无法从“ 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: 'karl.johnson@example.com',
  picture: {
    thumbnail: 'https://randomuser.me/api/portraits/thumb/men/62.jpg',
  },
},
{
  name: { title: 'mrs', first: 'asuncion', last: 'gomez' },
  email: 'asuncion.gomez@example.com',
  picture: {
    thumbnail: 'https://randomuser.me/api/portraits/thumb/women/52.jpg',
  },
  nat: 'ES',
},
{
  name: { title: 'miss', first: 'gilcenira', last: 'ribeiro' },
  email: 'gilcenira.ribeiro@example.com',
  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
  },

 });

如果它可以帮助某人...

1 个答案:

答案 0 :(得分:0)

您可以尝试在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)}
       />