Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native React Native FlatList不呈现项目错误_React Native_React Native Flatlist_Listitem - Fatal编程技术网

React native React Native FlatList不呈现项目错误

React native React Native FlatList不呈现项目错误,react-native,react-native-flatlist,listitem,React Native,React Native Flatlist,Listitem,嗨,我正在尝试在我的react本机页面中呈现一些用户信息,我不知道为什么它会呈现如下内容: 但是我的输出是 用户数据 export default [ { id: 1, name: 'Tiago Almeida', email: 'tiago@gmail.pt', avatarUrl: 'https://cdn.pixabay.com/photo/2013/07/13/10/07/man-

嗨,我正在尝试在我的react本机页面中呈现一些用户信息,我不知道为什么它会呈现如下内容:

但是我的输出是

用户数据

    export default [
    {
        id: 1,
        name: 'Tiago Almeida',
        email: 'tiago@gmail.pt',
        avatarUrl:
            'https://cdn.pixabay.com/photo/2013/07/13/10/07/man-156584_960_720.png',
    },
    {
        id: 2,
        name: 'Lucas Silva',
        email: 'lucas@gmail.com',
        avatarUrl:
            'https://cdn.pixabay.com/photo/2014/04/03/10/32/businessman-310819_960_720.png',
    },
    {
        id: 3,
        name: 'Andre Ferreira',
        email: 'andre@gmail.pt',
        avatarUrl:
            'https://cdn.pixabay.com/photo/2018/05/19/22/03/man-3414477_960_720.png',
    },];
这是我的主页

export default props => {
    function getActions(user) {
        return (
            <>
                <Button
                    onPress={() => props.navigation.navigate('UserForm', user)}
                    type='clear'
                    icon={<Icon name='edit' size={25} color='orange' />}
                />
            </>
        )
    }

    function getUserItem({ item: user }) {
        return (
            <ListItem
                leftAvatar={{ source: { uri: user.avatarUrl } }}
                key={user.id}
                tittle={user.name}
                subtitle={user.email}
                bottomDivider
                rightElement={getActions(user)}
                onPress={() => props.navigation.navigate('UserForm', user)}


            />
        )

    }

    return (
        <View>
            <FlatList
                keyExtractor={user => user.id.toString()}
                data={users}
                renderItem={getUserItem}
            />
        </View>
    )
};
导出默认道具=>{
函数getActions(用户){
返回(
props.navigation.navigate('UserForm',user)}
type='clear'
图标={}
/>
)
}
函数getUserItem({item:user}){
返回(
props.navigation.navigate('UserForm',user)}
/>
)
}
返回(
user.id.toString()}
数据={users}
renderItem={getUserItem}
/>
)
};

在导入文件的顶部

import { ListItem, Avatar } from 'react-native-elements';
之后,将代码更改为

您不需要
getActions

而是这样写,

 const getUserItem = ({ item: user }) => (
    <ListItem
      bottomDivider
      onPress={() => props.navigation.navigate('UserForm', user)}>
      <Avatar source={{ uri: user.avatarUrl }} />
      <ListItem.Content>
        <ListItem.Title>{user.name}</ListItem.Title>
        <ListItem.Subtitle>{user.email}</ListItem.Subtitle>
      </ListItem.Content>
      <ListItem.Chevron
        name="edit"
        size={25}
        color="orange"
        onPress={() => props.navigation.navigate('UserForm', user)}
      />
    </ListItem>
  );

  return (
    <View>
      <FlatList
        keyExtractor={(user) => user.id.toString()}
        data={users}
        renderItem={getUserItem}
      />
    </View>
  );
const getUserItem=({item:user})=>(
props.navigation.navigate('UserForm',user)}>

对于
版本2
,您似乎使用了错误版本的
react native elements
,您应该按如下方式更新
代码

import * as React from 'react';
import { Text, View, StyleSheet, FlatList } from 'react-native';
import Constants from 'expo-constants';
import { ListItem, Avatar, Button } from 'react-native-elements';
import Icon from 'react-native-vector-icons/FontAwesome';


// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';

const getActions = (user) => {
  return (
    <>
      <Button icon={<Icon name="edit" size={25} color="orange" />} />
    </>
  );
};

const UserItem = ({ item }) =>(
    <ListItem
      leftAvatar={{ rounded: true, source: { uri: item.avatarUrl } }}
      title={item.name}
      subtitle={item.email}
      rightElement={getActions(item)}
    />
);


export default function App() {
  return (
    <View
      style={{
        flex: 1,
      }}>
      <FlatList
        data={[
          {
            id: 1,
            name: 'Tiago Almeida',
            email: 'tiago@gmail.pt',
            avatarUrl:
              'https://cdn.pixabay.com/photo/2013/07/13/10/07/man-156584_960_720.png',
          },
          {
            id: 2,
            name: 'Lucas Silva',
            email: 'lucas@gmail.com',
            avatarUrl:
              'https://cdn.pixabay.com/photo/2014/04/03/10/32/businessman-310819_960_720.png',
          },
          {
            id: 3,
            name: 'Andre Ferreira',
            email: 'andre@gmail.pt',
            avatarUrl:
              'https://cdn.pixabay.com/photo/2018/05/19/22/03/man-3414477_960_720.png',
          },
        ]}
        renderItem={({ item, index }) => <UserItem item={item} />}
      />
    </View>
  );
}

Link

添加您的
ListItem
组件alsomy ListItem已经在我显示的页面上没有,我的意思是您创建了一个自定义
ListItem
组件,对吗?…我想要。没有,我使用react native的列表项没有创建成本您说您使用react native的ListItem,但从我看到的情况来看,ListItem不是react native包,但为react本机元素包
import * as React from 'react';
import {
  Text,
  View,
  StyleSheet,
  FlatList,
} from 'react-native';
import Constants from 'expo-constants';
import { ListItem, Avatar, Button } from 'react-native-elements'
import Icon from 'react-native-vector-icons/FontAwesome';

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';


const getActions = (user) => {
        return (
            <>
                <Button
                 
                    icon={<Icon name='edit' size={25} color='orange' />}
                />
            </>
        )
    }

const UserItem = ({ item }) => {
  return (
     <ListItem>
        <Avatar source={{uri: item.avatarUrl}} rounded />
        <ListItem.Content>
          <ListItem.Title>{item.name}</ListItem.Title>
          <ListItem.Subtitle>{item.email}</ListItem.Subtitle>
        </ListItem.Content>
          <View>
          {getActions(item)}
          </View>
      </ListItem>
  );
};

export default function App() {
  return (
    <View
      style={{
        flex: 1
      }}>
      <FlatList
        data={[
          {
            id: 1,
            name: 'Tiago Almeida',
            email: 'tiago@gmail.pt',
            avatarUrl:
              'https://cdn.pixabay.com/photo/2013/07/13/10/07/man-156584_960_720.png',
          },
          {
            id: 2,
            name: 'Lucas Silva',
            email: 'lucas@gmail.com',
            avatarUrl:
              'https://cdn.pixabay.com/photo/2014/04/03/10/32/businessman-310819_960_720.png',
          },
          {
            id: 3,
            name: 'Andre Ferreira',
            email: 'andre@gmail.pt',
            avatarUrl:
              'https://cdn.pixabay.com/photo/2018/05/19/22/03/man-3414477_960_720.png',
          },
        ]}
        renderItem={({ item, index }) => <UserItem item={item} />}
      />
    </View>
  );
}