reactjs - 无法读取未定义 react 17 的属性 'map'

标签 reactjs error-handling new-operator

目前,我开始在 Udemy 类(class)“react-for-the-rest-of-us”中学习 react 。
现在,我试图从子组件接近状态钩子(Hook),我得到了上述错误。
我的目标是通过从用户输入中获取它的值来向状态添加另一个元素
这是父组件:

    import React, { useState } from "react";
    import AddPetForm from "./FormPet";
    function Header(props) {
      const [pets, setPets] = useState([
        { name: "Meowsalot", species: "cat", age: "5", id: 123456789 },
        { name: "Barksalot", species: "dog", age: "3", id: 987654321 },
        { name: "Fluffy", species: "rabbit", age: "2", id: 123123123 },
        { name: "Purrsloud", species: "cat", age: "1", id: 456456456 },
        { name: "Paws", species: "dog", age: "6", id: 789789789 },
      ]);
    
      const pet = pets.map((pet) => (
        <Pet name={pet.name} species={pet.species} age={pet.age} id={pet.id} />
      ));
      return (
        <div>
          <LikedArea />
          <TimeArea />
          <ul>{pet}</ul>
          <AddPetForm set={setPets} />
        </div>
      );
    }
    function Pet(props) {
      return (
        <li>
          {props.name}is a {props.species} and is {props.age} years old
        </li>
      );
    }
这是子组件:
    import React, { useState } from "react";
    
    function AddPetForm(props) {
      const [name, setName] = useState();
      const [species, setSpecies] = useState();
      const [age, setAge] = useState();
      console.log(props.set);
      function handleSubmit(e) {
        e.preventDefault();
        props.set((prev) => {
          prev.concat({ name: name, species: species, age: age, id: Date.now() });
          setName("");
          setSpecies("");
          setAge("");
        });
      }
    
      return (
        <form onSubmit={handleSubmit}>
          <fieldset>
            <legend>Add New Pet</legend>
            <input
              value={name}
              onChange={(e) => setName(e.target.value)}
              placeholder="Name"
            />
            <input
              value={species}
              onChange={(e) => setSpecies(e.target.value)}
              placeholder="species"
            />
            <input
              value={age}
              onChange={(e) => setAge(e.target.value)}
              placeholder="age in years"
            />
            <button className="add-pet">Add Pet</button>
          </fieldset>
        </form>
      );
    }
    
    export default AddPetForm;

最佳答案

您没有返回新的串联数组。所以当你调用props.set应该是这样的:

props.set((prev) => {
      setName("");
      setSpecies("");
      setAge("");
      return prev.concat({ name: name, species: species, age: age, id: Date.now() });
});
如果您不返回任何内容,则从技术上讲,返回值为 undefined ,所以这就是它将状态设置为。然后,当您尝试 .map 时出现错误。它

关于reactjs - 无法读取未定义 react 17 的属性 'map',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64076133/

相关文章:

javascript - React redux 使用不同的调度更新单个属性与更新整个对象

javascript - 如何正确使用react-router

ruby-on-rails - 使用 webpack dev server 和 rails server 进行 webpack 热重载

php - 使用 PHP 处理验证错误的最简洁方法是什么?

grails - 如何知道从哪里抛出错误 500 (Grails)

c++ - 在 Windows 中,如何将 `GlobalAlloc` 替换为 `new` ?

javascript - react 路由器不渲染路由组件

r - 在lapply中使用tryCatch重试列表的元素

javascript - 为什么 "new function"调用该函数?

node.js - 在 node.js 中 require 如何与 new 运算符一起工作?