【问题标题】:Blank output in array looping in react native反应原生数组循环中的空白输出
【发布时间】:2019-04-17 17:20:57
【问题描述】:

以下代码的输出为空白。我无法弄清楚为什么没有打印问题甚至没有回答按钮。

尝试检查各种答案并更改呼叫方法。代码中没有显示错误。

const datas = [
        {
          id: 1,
          text: "Question 1",
          ans: [
            {
              id: 1,
              name: "Ans 1",
            },
            {
              id: 2,
              name: "Ans 2",
            }
          ]
        },
        {
          id: 2,
          text: "Question 2",
          ans: [
            {
              id: 1,
              name: "Ans 1",
            },
            {
              id: 2,
              name: "Ans 2",
            }
          ]
        },
        {
          id: 3,
          text: "Question 3",
          ans: [
            {
              id: 1,
              name: "Ans 1",
            },
            {
              id: 2,
              name: "Ans 2",
            }
          ]
        }
        ]
      ;

    return (
      <View style={styles.container}>
        <Text> Question 1</Text>
        <View style={styles.cardContainer}>
          <View style={styles.card}>
            {datas.map((data) => {
              <Text style={styles.quesText}> {data.text} </ Text>
                  { data.ans.map((answ) => {
                    <TouchableOpacity style={styles.choiceButton} onPress={ () => this.props.navigation.navigate('Quiz')}>
                    <Text style={styles.buttonText}>{answ.name}</Text>
                    </TouchableOpacity>
                  })}
              })}
              </View>
        </View>
      </View>
    )

}}

它应该显示: 问题一

还有两个带有 Ans 1 和 Ans 2 的按钮

【问题讨论】:

  • 当你将它限制为 {datas.map((data) => "Test"} 时会发生什么?你有没有得到任何输出?如果没有,那么问题就在映射之外功能。

标签: arrays loops react-native react-native-android


【解决方案1】:

在您的 map 用法中,您没有返回任何内容。因此,只需为每个内容添加一个return。另请注意,您不能返回多个孩子,而是将它们包装在 &lt;View&gt; 中以实现您的目标。这里有正确的代码:

return (
  <View style={styles.container}>
    <Text> Question 1</Text>
    <View style={styles.cardContainer}>
      <View style={styles.card}>
        {datas.map(data => {
          return (
            <View>
              <Text style={styles.quesText}>{data.text}</Text>
              {data.ans.map(answ => {
                return (
                  <TouchableOpacity
                    style={styles.choiceButton}
                    onPress={() => this.props.navigation.navigate('Quiz')}>
                    <Text style={styles.buttonText}>{answ.name}</Text>
                  </TouchableOpacity>
                );
              })}
            </View>
          );
        })}
      </View>
    </View>
  </View>
);

【讨论】:

  • 是的,我错过了回报。
猜你喜欢
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
  • 2018-03-19
  • 2020-06-29
  • 2021-05-01
相关资源
最近更新 更多