0

enter image description hereI am trying to map through the array that was stored in the state. The arrray had datas from firebase. I did console.log to see the what the state contains and it has the values but just not mapping it.

render() {
    let test = this.state.subjects.map(sub => {
      return (
        <View>
          <Text style={{ textAlign: "center" }}>{sub}</Text>
        </View>
      );
    });
    return <ScrollView style={{ marginTop: 30 }}>{test}</ScrollView>;
  }
}
6
  • Try to log sub in the console. Commented Dec 26, 2018 at 0:26
  • @MahediSabuj nothing shows Commented Dec 26, 2018 at 0:27
  • Can you show us the value of this.state.subjects? Commented Dec 26, 2018 at 0:31
  • [ 0: "This is a test 2" 1: "This is a test" length: 2 proto: Array(0) ] Commented Dec 26, 2018 at 0:34
  • I added a picture of the console result Commented Dec 26, 2018 at 0:37

4 Answers 4

1

Check below code:

renderSub() {
    return this.state.sub.map((value, index) => {
        return (
            <Text style={{ textAlign: "center" }}>{sub}</Text>
        )
    })
}
render() {
    return (
        <View>
            {this.renderSub()}
        </View>
    );
}
Sign up to request clarification or add additional context in comments.

Comments

0

So as you get an Object from firebase you have to convert it to an typeof Array to map over it - so you could do something like

Object.values(this.state.subjects).map(...) 

2 Comments

the data is pushed in the state array
[ 0: "This is a test 2" 1: "This is a test" length: 2 proto: Array(0)
0

You can use React-Native FlatList to show the values of array.

See the Docs: https://facebook.github.io/react-native/docs/flatlist

Comments

0

I was pushing the data directly to the state variable. But according to react native docs you should never do that and use ‘this.state.setState’ method.

Comments

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.