首页 > 解决方案 > 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}`];
       })
    );

我想在建议中添加多个项目,所以请帮助我

标签: reactjsreact-hooks

解决方案


您正在尝试映射一个空数组。填充后尝试映射它:

custName.map((items) => {
      return [`${items.name}`];
   })

推荐阅读