React native React Native FlatList不呈现项目错误
嗨,我正在尝试在我的react本机页面中呈现一些用户信息,我不知道为什么它会呈现如下内容: 但是我的输出是 用户数据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-
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>
);
}