渲染错误,元素类型无效:需要一个字符串(对于内置组件)

发布于 2025-01-11 21:28:43 字数 990 浏览 6 评论 0原文

渲染错误,元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义。您可能忘记导出组件。

在我使用了 React-native-elements 中的 List 和 ListItem 之后,我得到了这个错误。

import React ,{ useState,useEffect } from 'react';
import { Text, View,SafeAreaView,StyleSheet,TextInput,
Button,Alert,Image,FlatList} from 'react-native';
import {List,ListItem} from 'react-native-elements';
import Api from '../../../server/routes/userApi'

const Users = () => {
  
  const [usersData, setUsersData] = useState([])
  let api=new Api()

 const fetchData=async()=>{
   const users=await api.getUsers()
   console.log("users ",users)
  setUsersData(users)
 }
  useEffect(()=>{
    fetchData()
  },[])
return (
  <List>
    <FlatList
    data={usersData}
    renderItem={({item})=>(
      <ListItem
        roundAvatar
        title={`${item.firstName}`}
      />
    )}
    keyExtractor={item => item.id}
  />
  </List>
  )
}

export default Users;

render error, element type was invalid: expected a string (for built-in components) or a class/function (for composite components) but got :undefined. you likely forget to export your component.

after i used List and ListItem from react-native-elements i got this error.

import React ,{ useState,useEffect } from 'react';
import { Text, View,SafeAreaView,StyleSheet,TextInput,
Button,Alert,Image,FlatList} from 'react-native';
import {List,ListItem} from 'react-native-elements';
import Api from '../../../server/routes/userApi'

const Users = () => {
  
  const [usersData, setUsersData] = useState([])
  let api=new Api()

 const fetchData=async()=>{
   const users=await api.getUsers()
   console.log("users ",users)
  setUsersData(users)
 }
  useEffect(()=>{
    fetchData()
  },[])
return (
  <List>
    <FlatList
    data={usersData}
    renderItem={({item})=>(
      <ListItem
        roundAvatar
        title={`${item.firstName}`}
      />
    )}
    keyExtractor={item => item.id}
  />
  </List>
  )
}

export default Users;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2025-01-18 21:28:43

您正在使用 react-native-elements 自版本以来不再提供 List 组件1.0。

官方文档中提到了这一点。

列表组件已被删除!列表只是一个常规的 React Native 视图,带有一些小边距样式。实际上并不需要使用 ListItem 组件。相反,我们建议使用 React Native 中的 FlatList 或 SectionList 组件,它们既可以用作视图,也可以显示项目、拉动刷新等。

因此,导入语句

import {List, ListItem} from 'react-native-elements'

不起作用,因为 List 不存在。

只需将 List 组件替换为 View,根据需要设置必要的边距或其他样式,然后删除 List 的导入即可。由于您已经在使用 FlatListListItem 这应该可以工作。

You are using react-native-elements which does not provide a List component anymore since version 1.0.

This is mentioned in the official documentation.

List component has been removed! List was just a regular React Native View with some small margin styles. It wasn't actually needed to use the ListItem component. Instead we recommend using the FlatList or SectionList components from React Native which function both as Views and also displaying items, pull to refresh and more.

Hence, the import statement

import {List, ListItem} from 'react-native-elements'

does not work, since List does not exist.

Just replace your List component with a View, set the necessary margins or other styles as desired and remove the import of List. Since you are already using a FlatList and a ListItem this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文