简体   繁体   English

反应类型错误:this.state.List.map 不是 function

[英]React TypeError: this.state.List.map is not a function

 import React, { Component } from "react"; import Project from "./components/Project.js" import Form from "./Form.js"; class App extends Component { constructor(props) { super(props); this.state = { projectList: [], myProjects: [], userList: [], }; this.createProject = this.createProject.bind(this); } createProject(title, desc, langs, len, exp) { this.setState({ projectList: this.state.projectList.push( { title: title, description: desc, language: langs, length: len, experience: exp } ) }); } deleteProject(title) { const projects = this.state.projectList.filter( p => p.title;== title ). this;setState({projects}). } render() { return( <div> <Form createProject = {this.createProject} /> {this.state.projectList.map((params) => <Project {..;params}/>)} </div> ); } } export default App;

Hello, when running this, createProject gets passed as a prop to another class, which calls createProject with certain parameters.您好,在运行此程序时,createProject 将作为道具传递给另一个 class,该 class 使用某些参数调用 createProject。 However, upon trying to render, it returns this error.但是,在尝试渲染时,它会返回此错误。 TypeError: this.state.projectList.map is not a function.类型错误:this.state.projectList.map 不是 function。 Any advice on how to fix it?关于如何解决它的任何建议?

push returns the new length of the array, it doesn't return the array. push返回数组的新长度,它不返回数组。 So this code is replacing the array with a number:所以这段代码用一个数字替换了数组:

this.setState({
     projectList: this.state.projectList.push( // <============
         {
             title : title,
             description : desc,
             language : langs,
             length : len,
             experience : exp
         }
     )
 });

Numbers don't have a map method.数字没有map方法。 :-) :-)

You probably wanted to do this, which creates a new array with the new item added to it:您可能想要这样做,它会创建一个新数组,其中添加了新项目:

this.setState({
     projectList: [...this.state.projectList, {
         title : title,
         description : desc,
         language : langs,
         length : len,
         experience : exp
     }]
 });

You cannot declare state like this, you can do something like:您不能像这样声明 state,您可以执行以下操作:

        this.setState({
            projectList: [...this.state.projectList, (
                {
                    title : title,
                    description : desc,
                    language : langs,
                    length : len,
                    experience : exp
                }
            )]
        });
    Hi Adam,

    For the current problem below snippet would solve this issue

    this.setState({
                projectList: [...this.state.projectList, 
                    {
                        title : title,
                        description : desc,
                        language : langs,
                        length : len,
                        experience : exp
                    }
                ]
            });

For better understanding refer below link:
    https://www.robinwieruch.de/react-state-array-add-update-remove

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: undefined is not a function (evaluating 'this.state.list.map') - TypeError: undefined is not a function (evaluating 'this.state.list.map') 类型错误:this.state.contracts.map 不是函数 React - TypeError: this.state.contracts.map is not a function React 反应TypeError:this.state.descriptions.map不是函数 - React TypeError: this.state.descriptions.map is not a function 反应 | 类型错误:this.state.cars.map 不是函数 - React | TypeError: this.state.cars.map is not a function React TypeError:this.state.projects.map不是函数 - React TypeError: this.state.projects.map is not a function 反应类型错误 this.state.data.map 不是 function - React TypeError this.state.data.map is not a function React js simple get json和list contents“TypeError:this.state.items.map不是函数” - React js simple get json and list contents “TypeError: this.state.items.map is not a function” React:如何解决&#39;Uncaught TypeError:this.state.data.map不是一个函数&#39; - React : How to fix ' Uncaught TypeError: this.state.data.map is not a function' 反应:未捕获的TypeError:this.state.data.map不是函数,并且 - React: Uncaught TypeError: this.state.data.map is not a function and 仅在 state 更新后才反应“TypeError:imageCollection.map”不是 function - React “TypeError: imageCollection.map” is not a function only after state update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM