简体   繁体   English

错误:无法读取未定义的 REACT JS 的属性“地图”

[英]Error:Cannot read property 'map' of undefined REACT JS

When I delete an item, it displays cannot read notes, property of undefined.当我删除一个项目时,它显示无法读取笔记,未定义的属性。 Otherwise, the map function is working correctly.否则,地图功能正常工作。 I think there is some error in the filter function.我认为过滤器功能存在一些错误。

 import React, { useState } from "react";
import Header from "./Header";
import Footer from "./Footer";
import Note from "./Note";
import CreateArea from "./CreateArea";

function App() {
  const [notes, setNotes] = useState([]);

  function handleClick(note) {
    setNotes(function (prevItems) {
      return [...prevItems, note];
    });
    console.log("Hlllo");
    event.preventDefault();
  }

  function handleDelete(indexItem) {
    setNotes(function (prevItems) {
      notes.filter(function (note, index) {
        console.log(indexItem)
        return (index = indexItem)
      });
    });
  }

  return (
    <div>
      <Header />
      <CreateArea handleClick={handleClick} />
      {notes.map(function (note, index) {
        return (
          <Note
            key={index}
            indexItem={index}
            title={note.title}
            content={note.content}
            delete={handleDelete}
          />
        );
      })}

      <Footer />
    </div>
  );
}

export default App;

Here is the Create Area File,这是创建区域文件,

    import React, { useState } from "react";
    
    function CreateArea(props) {
      const [note, setNote] = useState({
        title: "",
        content: ""
      });
    
      function handleChange(event) {
        const { name, value } = event.target;
        setNote(function (prevValue) {
          return {
            ...prevValue,
            [name]: value
          };
        });
      }
    
      return (
        <div>
          <form>
            <input
              onChange={handleChange}
              name="title"
              placeholder="Title"
              value={note.title}
            />
            <textarea
              onChange={handleChange}
              name="content"
              placeholder="Take a note..."
              rows="3"
              value={note.content}
            />
            <button
              onClick={function () {
                {
                  props.handleClick(note);
                }
              }}
            >
              Add
            </button>
          </form>
        </div>
      );
    }
    
    export default CreateArea;

And here is the Note file,这是注释文件,

import React from "react";

function Note(props) {
  return (
    <div className="note">
      <h1>{props.title}</h1>
      <p>{props.content}</p>
      <button
        onClick={function () {
          props.delete(props.indexItem);
        }}
      >
        DELETE
      </button>
    </div>
  );
}

export default Note;

setNotes should return a new state. setNotes应该返回一个新状态。 If you don't return anything.如果你什么都不回。 The new state will be undefined .新状态将是undefined And this error will appear.并且会出现这个错误。 You can update like this to return a new element:您可以像这样更新以返回一个新元素:

setNotes(function (prevItems) {
  return notes.filter(function (note, index) {
    console.log(indexItem)
    return index === indexItem
  });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM