简体   繁体   English

我无法正确地将用户输入添加到 state,导致 map 不是 function 错误

[英]I am not able to add user input to state properly, getting map not a function error when it is

I have a simple list which is stored in the App component.我有一个简单的列表,它存储在 App 组件中。 This is used to display all the people and I want to be able to add new people to this list.这用于显示所有人,我希望能够将新人添加到此列表中。 I am not able to add input into my state, I am getting an error that map is not a function.我无法将输入添加到我的 state,我收到一个错误,即 map 不是 function。 Am i not creating the array properly?我没有正确创建数组吗?

const App = () => {
  const [persons, setPersons] = useState([
    {
      name: 'Artos Hellas',
      id: 1
    }
  ]);
  const [newName, setNewName] = useState('');

  const handleNewName = event => {
    setNewName(event.target.value);
  };
  const addName = event => {
    event.preventDefault();
    const personObject = {
      name: newName,
      id: persons.length + 1
    };
    setPersons(persons.concat(personObject));
    setPersons('');
  };

  const rows = () => persons.map(p => <li key={p.key}>{p.name}</li>);

  return (
    <div>
      <h2>Phonebook</h2>
      <form onSubmit={addName}>
        <div>
          name : <input value={newName} onChange={handleNewName} />
        </div>
        <div>
          <button type="submit">add</button>
        </div>
      </form>
      <h2>Numbers</h2>
      <ul>{rows()}</ul>
    </div>
  );
};

Remove the setPersons('');删除setPersons(''); statement, you might wanted to use setNewName('') :声明,您可能想使用setNewName('')

const addName = event => {
  event.preventDefault();
  const personObject = {
    name: newName,
    id: persons.length + 1
  };
  setPersons(persons.concat(personObject));
  // setPersons('');
  setNewName('');
};

Also, you got a wrong key prop while rendering list elements:此外,您在渲染列表元素时得到了错误的key prop:

//                                            v Not p.key
const rows = () => persons.map(p => <li key={p.id}>{p.name}</li>);

Moreover, it's confusing when you use a function named row and call it row() , you may try naming it as an action like renderRows or just use the ReactElement array:此外,当您使用名为row的 function 并将其命名为row()时,您可能会感到困惑,您可以尝试将其命名为类似renderRows的操作,或者只使用ReactElement数组:

const renderRows = () => persons.map(p => <li key={p.id}>{p.name}</li>);
<ul>{renderRows()}</ul>

// Or
const rows = persons.map(p => <li key={p.id}>{p.name}</li>);
<ul>{rows}</ul>

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

相关问题 当我将redux状态与组件状态映射时,出现错误 - when I map my redux state with component state I am getting an error .get 不是 function:当我获取 js controller 的输入值时,我遇到了错误 - .get is not a function: When i am getting the value of the input to js controller i am facing the error 输入字段为空时,我无法显示错误消息 - I am not able to show an error message when the input field is empty 将值推送到 state 时,我在 React Native 上遇到错误 - I am getting an error on React Native when pushing value to state 我的地图函数返回 React 组件时出错 - I am getting an error in my map function returning React component 为什么我收到“TypeError: this.state.users.map is not a function”用于已部署的 React 应用程序生产版本 - Why am I getting a “TypeError: this.state.users.map is not a function” for deployed production version of React application JavaScript,为什么会出现错误“函数add中未定义数组” - JavaScript, why am I getting the error “array is not defined in the function add” 收到错误 this.state.datas.map 不是 function - getting error this.state.datas.map is not a function 我得到一个&#39;这不是函数&#39;错误 - I am getting a 'this is not a function' error 当我执行 onSubmit 时,我无法显示 state - I am not able to display the state when I do onSubmit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM