首页 > 解决方案 > TypeError:projects.map 不是函数(React)

问题描述

成分

const Pcards = ({ projects }) => {
     return (
         <div>
             <CardColumns>
                 {projects.map((projects) => (
                    <Card>
                       <Card.Img variant="top" src={"http://localhost:8000" + projects.images[0].file_path + projects.images[0].file_name + projects.images[0].file_type} />

页面

class Projects extends Component {

    state = {
        projects:[]
    }

    componentDidMount() {
      fetch('http://localhost:5000/api/projects')
       .then(res => res.json())
       .then((data) => {
         this.setState({ projects: data })
       })
       .catch(console.log)
    }

    render () {
      return (
         <Pcards projects = {this.state.projects} />
      );
    }
}

新反应,此代码返回

TypeError:projects.map 不是函数

自从他编写了这段代码以来,这似乎在我的合作伙伴的一端编译得很好,我正在尝试扩展他的工作。

我看过其他类似的帖子,但找不到解决方法。知道这里发生了什么吗?

标签: javascriptreactjs

解决方案


尝试

class Projects extends Component {
    constructor(props){
        super(props)
        this.state = {
            projects:[]
        }
    }


推荐阅读