0

My output of following code is blank. I am not able to figure it out why question is not printed or even answer buttons.

Tried checking various answers and changing call methods. No error is shown in code.

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>
    )

}}

It should show: Question 1

and two buttons with Ans 1 and Ans 2

1
  • What happens when you limit it to just {datas.map((data) => "Test"} ? Are you getting any output at all? If not, then the issue is outside of the mapping function. Commented Apr 17, 2019 at 17:37

1 Answer 1

1

In both your map usages you are not returning anything. So, simply add a return for each content. Also note that you can't return multiple children, instead wrap them in a <View> to achieve your objective. Here you have the correct code:

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>
);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I was missing return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.