I have fetched the array using the graphql
query and stored it in a variable called mpbrands
. Now I want to store it in the state and render it in my component. I tried the below but its not giving any response
JavaScript
x
18
18
1
constructor(props) {
2
super(props)
3
this.state = {
4
count: 0
5
}
6
}
7
async componentDidMount(){
8
let brandQuery = BrandPageInstance.getBrandList();
9
await fetchQuery(brandQuery).then((mpbrand) => {
10
this.setState({
11
count: mpbrand.items
12
})
13
console.log(count)
14
},
15
(error) => console.log(error)
16
)
17
}
18
In the console I am getting an error Uncaught (in promise) ReferenceError: count is not defined
. My array structure is
JavaScript
1
4
1
mpbrand:
2
items: Array(10)
3
0: {default_value: "CHEVROLET", image: "image_url"}
4
Let me know how to do it. Since I am newbie not able to store it in the state
Advertisement
Answer
Try console.log(this.state.count)
That should solve the reference error.
count is part of the state object. So you can access it via this.state.count
.