1

我正在尝试从 firebase firestore 检索数据。来自 firestore 的数据正在控制台上登录,但在我使用组件时不显示。

renderList = () => {
        const { accounts } = this.props
        accounts && accounts.map((account) => {
            return (
                <View>
                    <Text>{account.accountName}</Text>
                    {console.log(account.accountName)}
                </View>
            )
        })
    }

    render() {
        return (
            <>
                {this.renderList()}
            </>
        )
    }

在上面的编码中,console.log(account.accountName) 正在工作,但它没有在 render 方法中打印。我需要使用该数据创建一个列表。

4

2 回答 2

0

请试试这个:

    renderList = () => {
            const { accounts } = this.props;
     if(accounts){
            return accounts.map((account) => {
                return (
                    <View>
                        <Text>{account.accountName}</Text>
                        {console.log(account.accountName)}
                    </View>
                )
            })
}
        }

        render() {
            return (
                <>
                    {this.renderList()}
                </>
            )
        }

希望能帮助到你

于 2020-03-23T05:34:43.670 回答
0

我也有同样的问题,这对我有用。在箭头 ( => ) 之后使用括号而不是像这样的花括号 ( => (... your code ...) not this => {....}

这是您的编辑代码

renderList = () => {
        const { accounts } = this.props
        accounts && accounts.map((account) => ( // don't use curly brace! use bracket "("
            return (
                <View>
                    <Text>{account.accountName}</Text>
                    {console.log(account.accountName)}
                </View>
            )
        )) // and here also; not curly brace; but a bracket ")"
    }

    render() {
        return (
            <>
                {this.renderList()}
            </>
        )
    }

这是我的形象

于 2021-11-28T16:59:42.040 回答