簡體   English   中英

React Native:平面列表和數組

[英]React Native : Flatlist and Array

這肯定是一個常見問題,但我無法使用數組源渲染 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 問題,然后出現另一個問題:從 'index.js. 我解決了它...而且...哦,是的....index.js 問題再次出現...pff...這是什么?

上次編輯...答案

最后,我意識到網上給出的一些例子不起作用; 語法錯誤被寫入並且無法運行它們。 我非常痛苦地解決了錯誤循環。 檢查我的整個代碼,在某處我做了一個導入: import {classX} from './classeX' ...我替換為: import classX from './classeX' 並且我的世界改變了......它起作用了......所以,經過大量測試,下面的代碼對我有用:

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
  },

 });

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

您可以嘗試在 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)}
       /> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM