【问题标题】:Data appears in console.log, but not with in a <Text>{}</Text> tag when trying to render a FlatList尝试呈现 FlatList 时,数据出现在 console.log 中,但未出现在 <Text>{}</Text> 标记中
【发布时间】:2018-02-22 05:46:18
【问题描述】:

我试图让一些特定数据出现在一些标签中,但它没有显示。但是,当我 console.log 我试图在 Text 标签中获取的相同数据时,我可以看到它。

这是我的代码

import React, { Component } from 'react';
import { 
        View,
        FlatList,
        Text
        } from 'react-native'

import { NavigationActions } from 'react-navigation';
import { Header } from '../common';
import Config from '../Config';

export default class Costco extends Component {
    constructor() {
        super();
        this.state = {
            stores: [],
        };
    }
    componentWillMount() {
        const obj = Config.costcoThree;
        const costcoArr = Object.values(obj);

        this.setState({
            stores: costcoArr,
        })
    }

    renderStores(item) {
        const navigate = this.props.navigation;
        console.log(item.branchName);
        return (
            <Text>{item.branchName}</Text>
        );
    }
    render(){
        return(

            <View>

                <Header headerText="Costco"/>
                <Text>Hello</Text>
                <FlatList
                    data={this.state.stores}
                    renderItem={({item}) => {
                        this.renderStores(item)
                        //return<Text>{item.branchName}</Text>
                    }}
                    keyExtractor={(item) => item.id}
                />
            </View>

        )
    }
}

我尝试了两种不同的方式渲染文本。第一个是通过在 renderItem 方法中返回 Text 标签之间的数据。这非常有效。但是,当我尝试返回函数 renderStores 时,我看不到文本出现。但是当我 console.log 时,数据确实出现了。

【问题讨论】:

    标签: react-native react-native-flatlist


    【解决方案1】:

    作为一个简短的答案,您需要在 FlatList 的 renderItem 方法中添加 return this.renderStores(item)

    如果您在函数中使用花括号 {},除非您在其中显式添加 return 语句,否则它不会返回任何内容。

    对于另一种解决方法,您可以删除花括号,它会自动返回您的 this.renderStores(item) 方法。

    如果您需要任何示例,请告诉我

    【讨论】:

    猜你喜欢
    • 2017-04-24
    • 2015-04-15
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多