reactjs - React (Map , Axios , useState) 使用带有 axios 的 map 添加 usestae 新值
问题描述
这是一个使用状态,所以我想使用地图添加更多客户
useEffect(() => {
getApiDAta()
async function getApiDAta() {
try {
const response = await axios.get('http://localhost:5000/custumer/')
.then(res => {setcustName(res.data)
custName.map((items) => {
setSuggestions([items.firstname])
console.log(suggestions);
})
})
} catch (error) {
console.error(error);
}
}
},[]);
const [custName,setcustName] = useState([])
const [suggestions,setSuggestions] = useState(
custName.map((items) => {
return [`${items.name}`];
})
);
我想在建议中添加多个项目,所以请帮助我
解决方案
您正在尝试映射一个空数组。填充后尝试映射它:
custName.map((items) => {
return [`${items.name}`];
})
推荐阅读
- laravel - Laravel 邮件在本地工作,但不在服务器上
- maven - Maven - 通过 groupId 更新依赖项的命令行
- triggers - 每当插入或删除新的联系人、机会记录时,触发以填充/更新客户记录上的这些字段
- javascript - 提交后验证表单 - Javascript
- javascript - Uncaught (in promise) TypeError: data.forEach 不是 LoadBooks 的函数
- php - 我无法使用 $_POST 从 mysql db 获取数据
- android - 在构建 Android 应用程序时禁用 CMake 目标
- joomla - 在模块中包含 js
- html - 在使用网格布局的 Html 中,当我们的图像少于一行时,如何使最后一个图像左对齐
- javascript - JS - 将函数执行限制为每秒一次