**I'm trying to learn react from some courses/videos and i got this error couple times and figure it out and solved but right now i can't figure it out. Can someone help me please about this? And also I tried not using "this" but it didn't work This is the error: **
Unhandled Rejection (TypeError): this.state.categories.map is not a function
CategoryList.render
C:/Users/Casper/Desktop/Programlar/React-Dersleri/intro/src/CategoryList.js:31
28 | <div>
29 | <h3>{this.props.info.title}</h3>
30 |
> 31 | <ListGroup>
| ^ 32 | {
33 | this.state.categories.map(category => (
34 | <ListGroupItem active={category.categoryName===this.props.currentCategory?true:false}
View compiled
▶ 18 stack frames were collapsed.
(anonymous function)
C:/Users/Casper/Desktop/Programlar/React-Dersleri/intro/src/CategoryList.js:21
18 | }
19 |
20 | getCategories = ()=>{
> 21 | fetch("http://localhost:3000/categories")
| ^ 22 | .then(response=>response.json())
23 | .then(data=>this.setState({categories:data}));;
24 | }
here is my code:
import React, { Component } from 'react'
import Navi from './Navi' //bulunduğum klasörden naviiyi import et
import CartSummary from './CartSummary'
import ProductList from './ProductList'
import {ListGroup,ListGroupItem} from 'reactstrap'
export default class CategoryList extends Component {
state = {
categories: []
};
componentDidMount(){
this.getCategories();
}
getCategories = ()=>{
fetch("http://localhost:3000/categories")
.then(response=>response.json())
.then(data=>this.setState({categories:data}));;
}
render() {
return (
<div>
<h3>{this.props.info.title}</h3>
<ListGroup>
{
this.state.categories.map(category => (
<ListGroupItem active={category.categoryName===this.props.currentCategory?true:false}
onClick={()=>this.props.changeCategory(category)}
key={category.id}>
{category.categoryName}
</ListGroupItem>
))
}
</ListGroup>
{ <h4>{this.props.currentCategory}</h4> }
</div>
)
}
}