【问题标题】:Nested <Text> tag not appearing in React Native App嵌套的 <Text> 标签未出现在 React Native App 中
【发布时间】:2021-09-23 12:21:35
【问题描述】:

我正在尝试使用 Javascript 有条件地在 React Native 应用程序上呈现一些文本,但由于某种原因它没有出现。

下面是我的代码:

import React from 'react';
import { StyleSheet, Text, View, TextInput } from 'react-native';
import firebase from '../firebase/firestore';


class FormBuilder extends React.Component {
    constructor() {
        super();
        this.firestoreRef = firebase.firestore().collection('Forms');
        this.state = {
          isLoading: true,
          formArr: []
        };
      }

    componentDidMount() {
        this.unsubscribe = this.firestoreRef.onSnapshot(this.getCollection);
    }

    componentWillUnmount(){
        this.unsubscribe();
    }



    getCollection = (querySnapshot) => {
        const formArr = [];
        querySnapshot.forEach((res) => {
          const { FormId, Company, CreatedBy, CreatedWhen, LastModified, LastModifiedBy, Payload } = res.data().formMetaData;
          formArr.push({
            FormId: FormId,
            Company: Company,
            CreatedBy: CreatedBy,
            CreatedWhen: CreatedWhen,
            LastModified: LastModified,
            LastModifiedBy: LastModifiedBy, 
            Payload: Payload
          });
        });
        this.setState({
            formArr,
          isLoading: false,
       });
    }

    render() {
        if (this.state.isLoading){
          return(
            <View style={styles.preloader}>
              <Text>Loading...</Text>
            </View>
          )
        }
        return(
            <View style={styles.dumb}>
                <Text>Form Data Below</Text>
                <View>
                    <Text>{
                        this.state.formArr.length > 0 ? this.state.formArr.map((item) => {
                            
                            <Text>Hello</Text>
                            

                        }) : <Text>No Forms in Database</Text>
                    
                    }</Text>
                </View>

            </View>
        )
    }
}


const styles = StyleSheet.create({
    container: {
        flex: 1,
        paddingBottom: 0
    },
    preloader: {
        paddingTop: 10,
        alignItems: 'center',
        justifyContent: 'center'
    },
    dumb: {
        borderWidth: 1,
    }

  })
  
  export default FormBuilder;

所以我正在从数据库接收数据,并在加载数据后显示该数据。我遇到问题的部分是这一行:

{this.state.formArr.length > 0 ? this.state.formArr.map((item) => {
   <Text>Hello</Text>

 }) : <Text>No Forms in Database</Text>
}

即使达到this.state.formArr.length &gt; 0条件,&lt;Text&gt;Hello&lt;/Text&gt;也不会出现在屏幕上。

我检查了是否达到了条件,因为如果我将&lt;Text&gt;Hello&lt;/Text&gt; 替换为alert(1),则会出现警报。

屏幕上唯一显示的是&lt;Text&gt;Form Data Below&lt;/Text&gt;

我是否遗漏了一些明显的东西?我知道条件正在执行,那为什么&lt;Text&gt; 标签不渲染呢?如果你们有任何问题,请告诉我。

【问题讨论】:

    标签: javascript react-native


    【解决方案1】:

    我在您的代码中看到的问题是,在映射一个数组后,您没有返回一个组件,这就是它没有在屏幕上显示任何内容的原因。

    试试这个

    {
      this.state.formArr.length > 0 ? (
        this.state.formArr.map(item => {
          return <Text>Hello</Text>;
        })
      ) : (
        <Text>No Forms in Database</Text>
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      相关资源
      最近更新 更多