简体   繁体   English

设置Flatlist numColumn> = 2时,我遇到问题,引发违规错误

[英]I am facing an issue while setting Flatlist numColumn >=2, throwing a violation error

I am facing an issue while setting Flatlist numColumn >=2, throwing a violation error. 设置Flatlist numColumn> = 2时,我遇到问题,引发违规错误。

Error : 错误:

Invariant Violation: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. 不变违反:不变违反:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。 You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports 您可能忘记了从定义文件中导出组件,或者您可能混淆了默认导入和命名导入

"axios": "^0.18.0",
 "react": "16.6.3",
 "react-native": "^0.58.4",
 "react-native-elements": "^1.0.0",
 "react-native-gesture-handler": "^1.0.15",
 "react-native-vector-icons": "^6.2.0",
 "react-navigation": "^3.1.2",

When set numColumn = {1} it runs perferctly fine, above value 1 it throws error 设置numColumn = {1}时,它运行得很好,超过值1会引发错误

<FlatList data={this.state.mdata}
              renderItem={this.renderList}
              keyExtractor={(item, index) => `${index}`}
              horizontal={false} numColumns={2}/>

ImageList.js ImageList.js

import {Text, View, ScrollView, TouchableOpacity,StyleSheet,Dimensions,FlatList} from 'react-native';
import  {Card}from 'react-native-elements';
import API from './axios';
const styles = StyleSheet.create({
    image: {
        flex:1,width:'50%',height:'50%',
        alignItems: 'center',
        justifyContent: 'center',
    },

})

export default class ImageList extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            mloading: false,
            mdata: [],
            current_page: 1,
            merror: null,
            mhasMore: true
        }
    }
    componentDidMount()  { this.getListOfPictures(); }

    render() {
        return (

            <FlatList
              data={this.state.mdata}
              renderItem={this.renderList}
              keyExtractor={(item, index) => `${index}`}
              horizontal={false} numColumns={2}/> 

           )
    }


    /********************************************************************************
     * getListOfPictures All pictures for dashboard
     ********************************************************************************/
    getListOfPictures = () => {
        if (this.state.mloading) { return; }
        this.setState({ mloading: true });

        API.get(`dashboard/all`)
            .then(res => {
                console.log("reco1m",res.data.recommendations[0]);
                const nextPictures= res.data.recommendations.map(data => ({
                    title: data.style,
                    image_url: data.img,
                    description: data.description,
                    id: data.style
                }));
                console.log(nextPictures);
                this.setState({
                    mhasMore: true,
                    mdata: [...this.state.mdata, ...nextPictures],
                    mloading: false,
                    current_page: this.state.current_page + 1})
            }).catch(error => {this.setState({ merror:error, mloading: false });});
    }

    isCloseToBottom({ layoutMeasurement, contentOffset, contentSize }) {
        return layoutMeasurement.height + contentOffset.y
        >= contentSize.height - 0;
    }

    renderList = () => {
        return ( this.state.mdata.map((u) => {
            return (
                <View style={{flex: 0.5, height: 150, margin: 5}}>
                <TouchableOpacity
                    key={u.id} >
                   <Card
                       featuredTitle={u.title}
                       image={{ uri: u.image_url }}
                       imageStyle={styles.image}>
                       <View style={{ padding: 10 }}>
                           <Text style={{ fontSize: 15}}>Description:</Text>
                           <Text>{u.description}</Text>
                       </View>
                   </Card>
                </TouchableOpacity>
                </View>
            );
        }))
    }


}

renderItem to FlatList accepts a function to which the item is passed, you need to render one element from it renderItemFlatList接受该item传递给的函数,您需要从中渲染一个元素

renderList = ({item: u}) => {

   return (
       <View style={{flex: 0.5, height: 150, margin: 5}}>
            <TouchableOpacity
                key={u.id} >
               <Card
                   featuredTitle={u.title}
                   image={{ uri: u.image_url }}
                   imageStyle={styles.image}>
                   <View style={{ padding: 10 }}>
                       <Text style={{ fontSize: 15}}>Description:</Text>
                       <Text>{u.description}</Text>
                   </View>
               </Card>
            </TouchableOpacity>
        </View>
    );
}

With numColumns={1} , FlatList is able to render the Mapped view returned from renderList but not with numColums={2} 使用numColumns={1}FlatList能够呈现从renderList返回的映射视图,但不能使用numColums={2}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在创建 React 应用程序时遇到以下问题 - I am facing the issue below while creating a React app 循环遍历动态表时遇到问题 - I am facing an issue while looping through a dynamic table {"message": "notNull Violation: users.firstName cannot be null,\nnotNull Violation: users.lastName cannot be null} 我面临的错误 - {"message": "notNull Violation: users.firstName cannot be null,\nnotNull Violation: users.lastName can not be null} error I am facing 我在通过 NPM 部署 React App 时遇到了一些漏洞问题 - I am facing some vulnerabilities issue while deploying React App via NPM 我在运行反应应用程序时遇到了这个问题。 如何解决这个问题? - I am facing this problem while running react application. how to solve this issue? 我试图通过复选框获取多选下拉列表,在填充项目并获取allChecked工作时遇到一些问题 - I am trying to get a multiselect dropdown with checkbox, facing some issue while populating items and getting allChecked work 我在咕unt中遇到以下错误 - I am facing the below error in grunt 如何将值从jquery ajax传递到servlet,传递URL时我面临404错误 - How to pass value from jquery ajax to servlet, I am facing 404 error while passing url 我面临一个错误 ReferenceError: Paginator is not defined - I am facing a error ReferenceError: Paginator is not defined 我在运行 reactjs 应用程序时遇到问题。下面给出的是错误 - I am facing a problem while running reactjs application.Given below is the error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM