Reactjs 反应:Can';找不到此错误的解决方案:未捕获类型错误:clients.map不是函数
你好:)我是新来的,有一个问题。。。我得到这个错误:Reactjs 反应:Can';找不到此错误的解决方案:未捕获类型错误:clients.map不是函数,reactjs,Reactjs,你好:)我是新来的,有一个问题。。。我得到这个错误: Uncaught TypeError: clients.map is not a function at App (index.js:34) 在我用钩子之前,一切都能用。。现在当状态改变时,我得到了这个错误。您可以在此处看到我的代码: // == Import npm import React, { useState } from 'react'; import Client from 'src/components/Client'
Uncaught TypeError: clients.map is not a function
at App (index.js:34)
在我用钩子之前,一切都能用。。现在当状态改变时,我得到了这个错误。您可以在此处看到我的代码:
// == Import npm
import React, { useState } from 'react';
import Client from 'src/components/Client';
import ClientForm from 'src/components/ClientForm';
// == Import
import './styles.css';
// == Composant
const App = () => {
const [clients, setClients] = useState([
{ id: 1, nom: 'Dimitri Basseguy' },
{ id: 2, nom: 'Bob Market' },
{ id: 3, nom: 'Kharn Pcp' },
]);
const handleDelete = (id) => {
const updatedClients = [...clients];
const index = updatedClients.findIndex((client) => client.id === id);
updatedClients.splice(index, 1);
setClients({ updatedClients });
};
const handleAdd = (client) => {
const updatedClients = [...clients];
updatedClients.push(client);
setClients({ updatedClients });
};
return (
<div>
<h1>Liste de clients</h1>
<ul>
{clients.map((client) => (
<Client
key={client.id}
details={client}
onDelete={handleDelete}
/>
))}
</ul>
<ClientForm onClientAdd={handleAdd} />
</div>
);
};
// == Export
export default App;
/==导入npm
从“React”导入React,{useState};
从“src/components/Client”导入客户端;
从“src/components/ClientForm”导入ClientForm;
//==导入
导入“./styles.css”;
//==镇静
常量应用=()=>{
const[clients,setClients]=useState([
{id:1,名称:'Dimitri Basseguy'},
{id:2,nom:'Bob Market'},
{id:3,名称:'Kharn Pcp'},
]);
常量handleDelete=(id)=>{
const updatedClients=[…clients];
const index=updatedClients.findIndex((client)=>client.id==id);
更新的clients.splice(索引,1);
setClients({updatedClients});
};
const handleAdd=(客户端)=>{
const updatedClients=[…clients];
updatedClients.push(客户端);
setClients({updatedClients});
};
返回(
客户名单
{clients.map((client)=>(
))}
);
};
//==出口
导出默认应用程序;
.map()到底是怎么回事
感谢您的帮助您需要更换:
setClients({ updatedClients });
到
因为您试图通过数组而不是对象映射
.map
。setClients
采用单个属性值,在本例中为数组,setClients(updatedClients)代码>
setClients(updatedClients);